有没有一种方法可以为两个相同 class 方法相同的一个泛型参数合并多重继承?
Is there a way to incorporate multiple inheritance for one generic parameter that is the same for two same class methods?
这两个泛型参数是一样的,但是我有两个不同的方法,它们使用两个不同的泛型参数类型约束。我正在尝试通过使用泛型来简化代码量。
public static void thisClass.MainMethod<T1, T2>() where T1 : Control where T2 : TextBoxBase
{
thisClass.FirstMethod<T1>(object x); // where T : Control is needed for ((T)x).OneMethod();
thisClass.SecondMethod<T2>(object x); // where T : TextBoxBase is needed for ((T)x).TwoMethod();
}
class是一个包含所有方法的静态class。有没有办法将其简化为以下内容?
public static void thisClass.MainMethod<T1>(object control) where T1 : Control, TextBoxBase
{
thisClass.FirstMethod<T1>(object objControl);
thisClass.SecondMethod<T1>(object objControl);
//more code here
}
编辑:这里是 FirstMethod 和 SecondMethod 的样子,以防有帮助。
public static void FirstMethod<T1>(object objControl) where T : Control
{
//some code here
string someStringVariableNeededInThisMethod = ((T1)objControl).Text; //not just this specific method needs to be called
//more code here
}
public static void SecondMethod<T1>(object objControl) where T : TextBoxBase
{
//some code here
((T1)objControl).AcceptsTab() = true; //not just this specific method needs to be called
//more code here
}
C# 自动将派生 classes 的对象视为基类 class 的实例。事实上,在这种情况下,您根本不需要泛型。
下面是一个例子。将 Base
视为 Control
,将 Derived
视为 TextBoxBase
(继承自 Control
):
using System;
namespace ConsoleApp3
{
public class Base
{
public int BaseInt { get; private set; } = 0;
public void IncrementBaseInt() => BaseInt++;
}
public class Derived : Base
{
public void PrintBaseInt() => Console.WriteLine($"{BaseInt}");
}
public static class Foo
{
public static void BaseMethod(Base argument) => argument.IncrementBaseInt();
public static void DerivedMethod(Derived argument) => argument.PrintBaseInt();
public static void Method(Derived argument)
{
DerivedMethod(argument);
BaseMethod(argument);
DerivedMethod(argument);
}
}
internal class Program
{
static void Main(string[] args)
{
Derived derivedArgument = new ();
Foo.Method(derivedArgument);
}
}
}
这个程序打印出 0
然后 1
。但是请注意,我可以使用 Derived
作为参数类型编写 BaseMethod
的签名,它会做完全相同的事情。
这两个泛型参数是一样的,但是我有两个不同的方法,它们使用两个不同的泛型参数类型约束。我正在尝试通过使用泛型来简化代码量。
public static void thisClass.MainMethod<T1, T2>() where T1 : Control where T2 : TextBoxBase
{
thisClass.FirstMethod<T1>(object x); // where T : Control is needed for ((T)x).OneMethod();
thisClass.SecondMethod<T2>(object x); // where T : TextBoxBase is needed for ((T)x).TwoMethod();
}
class是一个包含所有方法的静态class。有没有办法将其简化为以下内容?
public static void thisClass.MainMethod<T1>(object control) where T1 : Control, TextBoxBase
{
thisClass.FirstMethod<T1>(object objControl);
thisClass.SecondMethod<T1>(object objControl);
//more code here
}
编辑:这里是 FirstMethod 和 SecondMethod 的样子,以防有帮助。
public static void FirstMethod<T1>(object objControl) where T : Control
{
//some code here
string someStringVariableNeededInThisMethod = ((T1)objControl).Text; //not just this specific method needs to be called
//more code here
}
public static void SecondMethod<T1>(object objControl) where T : TextBoxBase
{
//some code here
((T1)objControl).AcceptsTab() = true; //not just this specific method needs to be called
//more code here
}
C# 自动将派生 classes 的对象视为基类 class 的实例。事实上,在这种情况下,您根本不需要泛型。
下面是一个例子。将 Base
视为 Control
,将 Derived
视为 TextBoxBase
(继承自 Control
):
using System;
namespace ConsoleApp3
{
public class Base
{
public int BaseInt { get; private set; } = 0;
public void IncrementBaseInt() => BaseInt++;
}
public class Derived : Base
{
public void PrintBaseInt() => Console.WriteLine($"{BaseInt}");
}
public static class Foo
{
public static void BaseMethod(Base argument) => argument.IncrementBaseInt();
public static void DerivedMethod(Derived argument) => argument.PrintBaseInt();
public static void Method(Derived argument)
{
DerivedMethod(argument);
BaseMethod(argument);
DerivedMethod(argument);
}
}
internal class Program
{
static void Main(string[] args)
{
Derived derivedArgument = new ();
Foo.Method(derivedArgument);
}
}
}
这个程序打印出 0
然后 1
。但是请注意,我可以使用 Derived
作为参数类型编写 BaseMethod
的签名,它会做完全相同的事情。