我希望能够在编译时使用具有给定约束的System.Type;
有没有一种优雅的方法来解决这个问题?
internal abstract class BaseClass {}
internal class SubClass : BaseClass {}
internal class OtherClass {}
internal class Consumer
{
public void DoSomething(Type pType) {}
public void DoSomething(BaseClass pBaseClass) {}
public void DoSomething<tBaseClass>(tBaseClass pBaseClass) where tBaseClass : BaseClass {}
}
[TestFixture()]
public class TypeConstraintTest
{
[Test()]
public void TestCase1()
{
var lConsumer = new Consumer();
lConsumer.DoSomething(typeof (SubClass));
// This should not be allowed. Should have a type constraint.
lConsumer.DoSomething(typeof (OtherClass));
lConsumer.DoSomething(null as SubClass);
// This will generate a compiler error, but it's
// not an elegant solution, not easily readable/understandable.
lConsumer.DoSomething(null as OtherClass);
}
}
希望这个其他的例子有助于澄清我的意图(道歉,如果不清楚我必须快速写下来):
[TestFixture()]
public class ExampleTest
{
internal interface GroupingInterface {}
internal interface TargetInterface {}
internal class Class1 : GroupingInterface, TargetInterface {}
internal class Class2 : GroupingInterface {}
[Test()]
void TestCase()
{
var lGroup = new List<GroupingInterface>() { new Class1(), new Class2() };
foreach(var lClass in lGroup)
{
this.TestMethod(lClass.GetType());
// This works, but we are passing the object just for forcing the type.
// We are not going to use the object itself, so it would be better not
// To pass the reference to the specific object if there is a way...
this.TestMethodWithInstance(lClass);
// Don't know the type at compile-time as it is variable.
//this.TargetMethodWithGeneric<???>
// Ideally, there should be something like a generic "variable" method:
//this.TargetMethodWithGeneric<typeFrom(lClass)>
// This should give a compiler error as a GroupingInterface is not a TargetInterface.
// But, if we pass any TargetInterface subtype it should compile.
}
}
void TestMethod(Type pType)
{
// At this point, we want to make sure pType is
// a subtype of TargetInterface at compile-time.
// SHOULD NOT BE AT RUNTIME, SHOULD NOT COMPILE IF WRONG TYPE PASSED:
if (pType.GetInterfaces().Contains(typeof (TargetInterface))) throw new Exception();
}
void TestMethodWithInstance(TargetInterface pClass)
{
var lSubType = pClass.GetType();
// Do something with the type...
}
void TargetMethodWithGeneric<tType>() where tType : TargetInterface
{
// Do something with tType.
}
}
答案 0 :(得分:1)
在编译时没有办法做你要问的事情。 System.Type
是预期的类型,然而,对于您的通用我会认为它正是您想要的解决方案。通常,您不会直接将null传递给方法,它将是某种类型的变量,从而消除了强制转换的要求。如果你想要做的事情实际上不是一个参数而是一个类型,你可以改变定义以更好地匹配。
public void DoSomething<tBaseClass>() where tBaseClass : BaseClass
{
}
然后调用者只需指定类型。
lConsumer.DoSomething<OtherClass>();
我仍然不理解除了你所拥有的东西之外的东西。即使它是其他类型的列表,您也可以使用Linq使用OfType<T>()
将其过滤为您感兴趣的类型。
[Test()]
public void TestCase()
{
var lGroup = new List<GroupingInterface>() { new Class1(), new Class2() };
// If you know you need to extract classes of a certain type you can use this:
foreach (var lclass in lGroup.OfType<TargetInterface>())
{
// using OfType means lclass is already cast as the type expected, if the object is not of that type it will not be iterated
TestMethodWithInstance(lclass);
}
}
答案 1 :(得分:0)
您是否正在寻找运行时检查:
if( !typeof(BaseClass).IsAssignableFrom(t) )
throw new Exception("Must be BaseClass derivative");
答案 2 :(得分:0)
为什么要使用泛型而不是仅仅定义要传入的接口或基类?当您需要特定类型时,没有理由使用泛型。