C# 给定一个受约束的泛型方法,我可以调用传递泛型参数实际类型的非泛型方法吗

C# 给定一个受约束的泛型方法,我可以调用传递泛型参数实际类型的非泛型方法吗,c#,.net,generics,types,type-conversion,C#,.net,Generics,Types,Type Conversion,我知道这个标题很尴尬,最好用代码来解释。给定一组类: public abstract class MyBaseType { public string Message { get; set; } } public class MySuperType : MyBaseType { public string AdditionalInfo { get; set; } } public class MyOtherSuperType : MyBaseType { public

我知道这个标题很尴尬,最好用代码来解释。给定一组类:

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 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 });

但这感觉很不雅。有更好的方法吗?

这里的问题是,您并没有真正表达一种普遍性。您必须为每个基本类型实现
OutputFormattedTypeInfo
,因此您最好忘记泛型方法,只需使用重载即可(此处不需要泛型):


正如Aliostad所说,您尝试做的不再是泛型的,最好只使用重载。看起来你正在尝试类似于C++中模板特化的东西,在那里依赖于泛型类型,它调用不同的方法。 这里有一个例子,我使用反射实现了一种泛型专门化,如果重载方法不适合您,您可能可以应用类似的模式。如果您可以缓存反射的结果,并且只调用一次GetMethod,那么它就不会太慢。在T的泛型类中,有一个方法调用:

if (_serializeDataToStream == null)
    _serializeDataToStream = (Action<BinaryWriter, int, T[]>)GetTypeSpecificSerializationMethod();

_serializeDataToStream(writer, _size, _data);
if(_serializedDataToStream==null)
_serializeDataToStream=(操作)GetTypeSpecificSerializationMethod();
_序列化DataToStream(写入程序、\u大小、\u数据);
其中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]);
    }
}
//
///返回指向特定于静态类型的序列化方法的委托
/// 
/// 
私有委托GetTypeSpecificDeserialization方法()
{
如果(类型(T)=类型(双))
{
MethodInfo method=this.GetType().GetMethod(“反序列化双精度”,BindingFlags.Static | BindingFlags.NonPublic);
返回Delegate.CreateDelegate(typeof(Action),method);
}
否则如果(typeof(T)=typeof(ushort))
{
MethodInfo method=this.GetType().GetMethod(“反序列化Ushort”,BindingFlags.Static | BindingFlags.NonPublic);
返回Delegate.CreateDelegate(typeof(Action),method);
}
else if(typeof(T)=typeof(DateTime))
{
MethodInfo method=this.GetType().GetMethod(“反序列化日期时间”,BindingFlags.Static | BindingFlags.NonPublic);
返回Delegate.CreateDelegate(typeof(Action),method);
}
否则如果(typeof(T)=typeof(bool))
{
MethodInfo method=this.GetType().GetMethod(“反序列化工具”,BindingFlags.Static | BindingFlags.NonPublic);
返回Delegate.CreateDelegate(typeof(Action),method);
}
抛出新的NotImplementedException(“没有为类型“+typeof(T).FullName”设置反序列化方法);
}
/// 
///将double[]序列化为BinaryWriter
/// 
/// 
/// 
/// 
私有静态void序列化double(BinaryWriter编写器,int size,double[]数据)
{
对于(int i=0;i
-1这将始终使用基类重载调用方法,而不是专门的类。我的朋友,你错了。如果你经过一只长颈鹿,并且你有长颈鹿和动物的过载,它将使用正确的过载。是的,但是如果你正在处理它们的集合,比如说IEnumerable,它将适用于动物,无论它是长颈鹿还是袋熊。但是,对于正确派生类型的单个T,您是正确的。
/// <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]);
    }
}