【问题标题】:Method parameter to accept multiple types接受多种类型的方法参数
【发布时间】:2016-01-25 05:08:40
【问题描述】:

我正在开发一个应用程序,其中有多种类型的RichTextBoxs,我已经自定义了(RichTextBox,RichAlexBox,TransparentRichTextBox)

我想创建一个方法来接受所有这些类型以及一些其他参数。

private void ChangeFontStyle(RichTextBox,RichAlexBox,TransparentRichTextBox rch,
                                                         FontStyle style, bool add)
{
  //Doing somthing with rch.Rtf
}

我通过 StackOverFlow 搜索并找到了一些答案 like this 我无法弄清楚如何使用它们来解决我的问题

void foo<TOne, TTwo>()  //There's just one parameter here
   where TOne : BaseOne //and I can't figure out how to define my other two parameters        
   where TTwo : BaseTwo

我也尝试过重载this answer 提供的服务,

private void ChangeFontStyle(TransparentRichTextBox rch, FontStyle style, bool add);
private void ChangeFontStyle(RichAlexBox rch, FontStyle style, bool add);
private void ChangeFontStyle(RichTextBox rch,FontStyle style, bool add)
  {
    //Some codes
  }

但是我遇到了这个错误

'ChangeFontStyle(RichAlexBox, FontStyle, bool)' 
         must declare a body because it is not marked abstract, extern, or partial

这是我检查的其他一些问答:

Generic method with multiple constraints

Can I create a generic method that accepts two different types in C#

C# generic method with one parameter of multiple types

任何建议都将不胜感激。

【问题讨论】:

    标签: c# winforms generics


    【解决方案1】:

    假设 TransparentRichTextBoxRichAlexBox 都是 RichTextBox,那么你只需要一个方法签名:

    private void ChangeFontStyle(RichTextBox rch, FontStyle style, bool add)
    {
        //Doing somthing with rch.Rtf
    }
    

    否则,您将需要为每个重载实现该方法:

    private void ChangeFontStyle(TransparentRichTextBox rch, FontStyle style, bool add)
    {
        //Some codes
    }
    
    private void ChangeFontStyle(RichAlexBox rch, FontStyle style, bool add)
    {
        //Some codes
    }
    
    private void ChangeFontStyle(RichTextBox rch,FontStyle style, bool add)
    {
        //Some codes
    }
    

    【讨论】:

      【解决方案2】:

      如果您正确使用继承,则不需要泛型或重载:

      class RichTextBox
      {
           // implementation
      }
      
      class RichAlexBox : RichTextBox
      {
           // implementation
      }
      
      class TransparentRichTextBox : RichTextBox
      {
           // implementation
      }
      
      // allows passing in anything that inherits from RichTextBox
      private void ChangeFontStyle(RichTextBox rch, FontStyle style, bool add)
      {
           // implementation
      }
      

      正如@dotnetkid 建议的那样,另一种选择是将ChangeFontStyle 设为RichTextBox 类中的一个方法并将其设为虚拟,以便您在需要时可以覆盖它:

      class RichTextBox
      {
           public virtual void ChangeFontStyle(FontStyle style, bool add)
           {
               // implementation
           }
      
           // implementation
      }
      
      class RichAlexBox : RichTextBox
      {
           // uses the inherited ChangeFontStyle
      
           // implementation
      }
      
      class TransparentRichTextBox : RichTextBox
      {
           public override void ChangeFontStyle(FontStyle style, bool add)
           {
               // TransparentRichTextBox-specific implementation
           }
      
           // implementation
      }
      

      【讨论】:

      • 我建议将 ChangeFontStyle 设置为 RichTextBox 并将其标记为 Virtual
      • @dotnetkid 这是有史以来最好的报价!
      【解决方案3】:

      当重载一个方法时这样做。

      private void ChangeFontStyle(TransparentRichTextBox rch, FontStyle style, bool add)
      {
         // your code
      }
      
      private void ChangeFontStyle(RichAlexBox rch, FontStyle style, bool add) 
      {
         // your code
      }
      private void ChangeFontStyle(RichTextBox rch,FontStyle style, bool add)
      {
         // your code
      }
      

      或者你可以像这样使用Control类作为参数试试

       private void ChangeFontStyle(Control control,FontStyle style, bool add)
       {
             // your code
       }
      

      您可以传递任何您想要的控件,例如 TextBoxRichTextBoxComboBox 等。

      【讨论】:

        猜你喜欢
        • 2016-09-23
        • 1970-01-01
        • 2016-02-29
        • 2012-03-25
        • 2012-09-23
        • 2012-06-02
        • 2023-01-05
        • 2012-04-12
        • 2021-06-11
        相关资源
        最近更新 更多