我知道尴尬的标题,最好在代码中解释。给定一组类:
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; }
}
有没有办法编写一个泛型方法,该方法调用传递泛型类型的非泛型方法,同时将传递的类型解释为其实际类型而不是基类型。也就是说,我想要写这样的东西:
public void OutputFormattedTypeInfo<T>(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));
}
但显然将Type作为T解释为基本类型。我知道我可以使用这样的反射:
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 });
但它感觉不够优雅。还有更好的方法吗?
答案 0 :(得分:1)
这里的问题是你并没有真正表达一般性。您必须为每种基本类型实施OutputFormattedTypeInfo
,因此您可能会忘记通用方法,只需使用重载(此处不需要泛型 EM>):
public void OutputFormattedTypeInfo(BaseType theType)
{
// ...
}
public void OutputFormattedTypeInfo(MySuperType theType)
{
// ...
}
public void OutputFormattedTypeInfo(MyOtherSuperType theType)
{
// ...
}
答案 1 :(得分:1)
正如Aliostad所说,你试图做的事情不再是通用的,只是使用重载会更好。看起来你正在尝试做类似于C ++中的模板特化的东西,取决于它调用不同方法的泛型类型。
这是一个使用反射实现了一种泛型特化的示例,如果重载方法不适合您,您可以应用类似的模式。如果你可以缓存反射的结果并且只调用一次GetMethod,那么事实证明它不会太慢。在T类通用的内部,有一个方法调用:
if (_serializeDataToStream == null)
_serializeDataToStream = (Action<BinaryWriter, int, T[]>)GetTypeSpecificSerializationMethod();
_serializeDataToStream(writer, _size, _data);
GetTypeSpecific方法使用反射来创建委托
/// <summary>
/// Returns a delegate that points at the static type specific serialization method
/// </summary>
/// <returns></returns>
private Delegate GetTypeSpecificDeserializationMethod()
{
if (typeof(T) == typeof(double))
{
MethodInfo method = this.GetType().GetMethod("DeserializeDouble", BindingFlags.Static | BindingFlags.NonPublic);
return Delegate.CreateDelegate(typeof(Action<BinaryReader, int, T[]>), method);
}
else if (typeof(T) == typeof(ushort))
{
MethodInfo method = this.GetType().GetMethod("DeserializeUshort", BindingFlags.Static | BindingFlags.NonPublic);
return Delegate.CreateDelegate(typeof(Action<BinaryReader, int, T[]>), method);
}
else if (typeof(T) == typeof(DateTime))
{
MethodInfo method = this.GetType().GetMethod("DeserializeDateTime", BindingFlags.Static | BindingFlags.NonPublic);
return Delegate.CreateDelegate(typeof(Action<BinaryReader, int, T[]>), method);
}
else if (typeof(T) == typeof(bool))
{
MethodInfo method = this.GetType().GetMethod("DeserializeBool", BindingFlags.Static | BindingFlags.NonPublic);
return Delegate.CreateDelegate(typeof(Action<BinaryReader, int, T[]>), method);
}
throw new NotImplementedException("No deserialization method has been setup for type " + typeof(T).FullName);
}
/// <summary>
/// Serialize double[] to BinaryWriter
/// </summary>
/// <param name="writer"></param>
/// <param name="size"></param>
/// <param name="data"></param>
private static void SerializeDouble(BinaryWriter writer, int size, double[] data)
{
for (int i = 0; i < size; i++)
{
writer.Write(data[i]);
}
}