要创建每种类型的类,我需要某种先决条件来创建它们以进行渲染,这是我在静态方法中编写的。
public abstract class ClassBase {
public static double ValueModifier (double value) {
return value;
}
}
public class ClassChildA : ClassBase {
public new static double ValueModifier (double value) {
return Math.Sign(value)*10;
}
}
public class ClassChildB : ClassBase {
public new static double ValueModifier (double value) {
return Math.Round(value/0.5)*0.5;
}
}
public class Printer<T> where T : ClassBase {
public double Print (double value) {
// T' is a type parameter, which is not valid in the given context
return T.ValueModifier(value);
// 'Type' does not contain a definition for 'ValueModifier' and no accessible extension method 'ValueModifier' accepting a first argument of type 'Type' could be found (are you missing a using directive or an assembly reference?)
return typeof(T).ValueModifier(value);
}
}
方法应该怎么写Print?
还是最好在每种类型的单独类中描述这些条件ClassChild?
这就是它的工作原理,只能通过反思。
我们试试看
结论