给定约束generics方法,我可以调用非generics方法传递generics参数的实际类型
我知道尴尬的标题,最好在代码中解释。 给定一组类:
public abstract class MyBaseType { public string Message { get; set; } } public class MySuperType : MyBaseType { public string AdditionalInfo { get; set; } } public class MyOtherSuperType : MyBaseType { public DateTime Started { get; set; } public DateTime Finished { get; set; } }
有没有办法编写一个generics方法,该方法调用传递generics类型的非generics方法,同时将传递的类型解释为其实际类型而不是基类型。 也就是说,我想写这样的东西:
public void OutputFormattedTypeInfo(T theType) where T : MyBaseType { OutputFormattedTypeInfo(theType as T); } public void OutputFormattedTypeInfo(MySuperType theType) { System.Console.WriteLine(String.Format("{0} and {1}", theType.Message, theType.AdditionalInfo)); } public void OutputFormattedTypeInfo(MyOtherSuperType theType) { System.Console.WriteLine(String.Format("{0} - Start: {1}, End: {2}", theType.Message, theType.Started, theType.Finished)); }
但很明显,作为T的Type被解释为基本类型。 我知道我可以使用这样的reflection:
Type type = typeof(MyBaseTypeDisplayFormatter); MethodInfo method = type.GetMethod( "FormatSpecific", BindingFlags.Instance | BindingFlags.NonPublic, null, new[] { update.GetType() }, null); return (MyBaseTypeDataItem)method.Invoke(this, new object[] { update });
但它只是感觉不够优雅。 有没有更好的办法?
正如Aliostad所说,你试图做的事情不再是通用的,只是使用重载会更好。 看起来你正在尝试做类似于C ++中的模板特化的东西,它取决于它调用不同方法的generics类型。
下面是我使用reflection实现一种通用特化的示例,如果重载方法不适合您,您可以应用类似的模式。 如果你可以缓存reflection的结果并且只调用一次GetMethod,那么事实certificate它不会太慢。 在T类通用内部,有一个方法可以调用:
if (_serializeDataToStream == null) _serializeDataToStream = (Action)GetTypeSpecificSerializationMethod(); _serializeDataToStream(writer, _size, _data);
GetTypeSpecific方法使用reflection创建委托的位置
/// /// Returns a delegate that points at the static type specific serialization method /// /// private Delegate GetTypeSpecificDeserializationMethod() { if (typeof(T) == typeof(double)) { MethodInfo method = this.GetType().GetMethod("DeserializeDouble", BindingFlags.Static | BindingFlags.NonPublic); return Delegate.CreateDelegate(typeof(Action), method); } else if (typeof(T) == typeof(ushort)) { MethodInfo method = this.GetType().GetMethod("DeserializeUshort", BindingFlags.Static | BindingFlags.NonPublic); return Delegate.CreateDelegate(typeof(Action), method); } else if (typeof(T) == typeof(DateTime)) { MethodInfo method = this.GetType().GetMethod("DeserializeDateTime", BindingFlags.Static | BindingFlags.NonPublic); return Delegate.CreateDelegate(typeof(Action), method); } else if (typeof(T) == typeof(bool)) { MethodInfo method = this.GetType().GetMethod("DeserializeBool", BindingFlags.Static | BindingFlags.NonPublic); return Delegate.CreateDelegate(typeof(Action), method); } throw new NotImplementedException("No deserialization method has been setup for type " + typeof(T).FullName); } /// /// Serialize double[] to BinaryWriter /// /// /// /// private static void SerializeDouble(BinaryWriter writer, int size, double[] data) { for (int i = 0; i < size; i++) { writer.Write(data[i]); } }
这里的问题是你并没有真正表达一般性。 您必须为每个基本类型实现OutputFormattedTypeInfo
,因此您可能会忘记generics方法并简单地使用重载 ( 此处不需要generics ):
上述就是C#学习教程:给定约束generics方法,我可以调用非generics方法传递generics参数的实际类型分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注---计算机技术网(www.ctvol.com)!
public void OutputFormattedTypeInfo(BaseType theType) { // ... } public void OutputFormattedTypeInfo(MySuperType theType) { // ... } public void OutputFormattedTypeInfo(MyOtherSuperType theType) { // ... }
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/953201.html