【发布时间】: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
任何建议都将不胜感激。
【问题讨论】: