方法参数接受多种类型 [英] Method parameter to accept multiple types

查看:151
本文介绍了方法参数接受多种类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,该应用程序在我定制的(RichTextBox,RichAlexBox,TransparentRichTextBox)中有多种类型的 RichTextBox / code>。



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

  private void ChangeFontStyle(RichTextBox,RichAlexBox,TransparentRichTextBox rch,
FontStyle style,bool add)
{
//使用rch.Rtf
}

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

  void foo< TOne,TTwo>()//这里只有一个参数
其中TOne:BaseOne / /我无法弄清楚如何定义我的另外两个参数e
TTwo:BaseTwo

我也试过重载为这个答案提供,

  private void ChangeFontStyle(TransparentRichTextBox rch,FontStyle风格,bool add); 
private void ChangeFontStyle(RichAlexBox rch,FontStyle style,bool add);
private void ChangeFontStyle(RichTextBox rch,FontStyle style,bool add)
{
//某些代码
}

但是我遇到这个错误
$ b $ pre $ ChangeFontStyle(RichAlexBox,FontStyle, bool)'
必须声明一个实体,因为它没有被标记为抽象的,extern的或者部分的

以下是一些其他Q& AI检查:

具有多个约束的泛型方法





C#通用方法,带一个参数多个类型



假设 TransparentRichTextBox

RichAlexBox 都是
RichTextBox ,那么您只需要一个方法签名:


  private void ChangeFontStyle(RichTextBox rch,FontStyle style,bool add)
{
//用rch.Rtf $ b $做一些事情}

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

  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)
{
// Som e代码
}


I'm developing an application which in I got multiple types of RichTextBoxs which I've customized (RichTextBox,RichAlexBox,TransparentRichTextBox).

I want to create a method to accept all those types plus some other parameters.

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

I've searched through StackOverFlow and found some answers like this which I couldn't figure out how to use them to solve my problem

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

I also tried overloading as this answer offers,

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
  }

But I encounter this error

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

Here's some other Q&A I checked:

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

Any suggestion would be appreciated.

解决方案

Assuming that TransparentRichTextBox and RichAlexBox are both subtypes of RichTextBox, then you only need one method signature:

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

Otherwise, you will need to implement the method for every overload:

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
}

这篇关于方法参数接受多种类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆