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

2023-12-10

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

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

有没有一种方法可以编写一个泛型方法,该方法调用传递泛型类型的非泛型方法,同时将传递的类型解释为其实际类型而不是基类型。也就是说,我want写这样的东西:

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

但显然 theType as 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 });

但就是感觉不优雅。有没有更好的办法?


正如阿利奥斯塔所说,您尝试做的事情不再是通用的,最好只使用重载。看起来您正在尝试执行类似于 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]);
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐