大佬教程收集整理的这篇文章主要介绍了C# - 检查泛型类型约束中的编译级别参数 | ... 其中 T : new( Foo a ),大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
泛型类型中众所周知的约束是 new(),可以添加像 (args,args,...) 这样的参数来强制编译器检查该类是否包含特定的构造函数?
该块向您展示了案例。
public class FooType
{
public FooType(int arg1)
{
}
}
public sealed class FooTypeChildA : FooType
{
public FooTypeChildA(int a) : base(a)
{
}
}
public sealed class FooTypeChildB : FooType
{
public FooTypeChildB() : base(0)
{
}
}
//public class FooConsTraint<T> where T : FooType,new() // succesS
//public class FooConsTraint<T> where T : FooType,new(int) // ERROR
public class FooConsTraint<T> where T : FooType // usually the consTraint is "new()",but I need something like this: new(int) that the compiler verify the checK_1
{
}
public sealed class Passed : FooConsTraint<FooTypeChildA> //[checK_1] Pass the consTraint.
{
}
public sealed class NotPassed : FooConsTraint<FooTypeChildB> //[checK_1] Not Pass the consTraint.
{
}
这个指令显示了一个可能的语法异常, new(int arg1)
泛型实例调用构造函数的方式并不重要,因为基本反射在运行时解决了问题,但其想法是强制编译器错误。
ATTEMPT#1 - 这是不可能的;因为接口不能指定构造函数。
public interface IFoo
{
IFoo(int a); // Error here CS0526
}
ATTEMPT#2 - 原始问题已关闭,因为版主专注于在 运行时 级别而不是 编译时 级别解决问题水平。
This Question is not duplicated in this question: 因为 (T)Activator.CreateInstance( typeof(T),args ) 当您需要非常严格的编译检查时不是一个选项.很明显,Activator 没有完成基本原则(必须使用派生类和继承行为,而不是它们的最终实现 - liskov)保证子类型具有的行为父类型甚至在编译级别。
泛型类型中众所周知的约束是 new(),可以添加像 (args,args,...) 这样的参数来强制编译器检查该类是否包含特定的构造函数?
虽然感觉这应该可用,但事实并非如此。您只能为无参数构造函数添加约束。
作为替代方案,考虑传递工厂函数。在这里,您可以指定输入参数。
public class FooConsTraint<T> where T : FooType
{
private readonly T _myObject;
public FooConsTraint(Func<int,T> generator)
{
_myObject = generator(123);
}
}
注意:您也可以存储 Func
本身,而不是立即使用它并丢弃它。这只是一个例子。
由于 FooConsTraint
类定义明确定义了 Func<>
泛型参数,因此您可以确保 FooConsTraint
的使用者确切知道您的 FooConsTraint
将使用哪些构造函数参数实例化一个 T
实例。
var myConsTraint = new FooConsTraint<FooTypeChildA>(myInt => new FooTypeChildA(myint));
因为你总是只调用一个构造函数而不用 Func
做任何花哨的事情,这感觉有点像样板,但考虑到没有真正的泛型参数化构造函数约束,这是最接近的替代。
以上是大佬教程为你收集整理的C# - 检查泛型类型约束中的编译级别参数 | ... 其中 T : new( Foo a )全部内容,希望文章能够帮你解决C# - 检查泛型类型约束中的编译级别参数 | ... 其中 T : new( Foo a )所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。