C# 反射:为泛型方法提供 T

发布于 2024-12-29 06:03:33 字数 1173 浏览 1 评论 0原文

我没有使用反射和泛型方法的经验,这里有两种方法。我想你能理解我想做的事情。

public static T GetInHeaderProperty<T>() where T : new()
{
    dynamic result = new T();

    result.CompanyId = ConfigurationManager.AppSettings["CompanyId"];
    result.UserId = ConfigurationManager.AppSettings["UserId"];
    result.Password = ConfigurationManager.AppSettings["Password"];
    result.MessageId = ConfigurationManager.AppSettings["MessageId"];

    Type platformType = typeof(T).GetProperty("PlatformType").PropertyType;
    // Here is my problem, I can not compile my code because of this line
    result.PlatformType = (dynamic)GetPlatformType<platformType>();
    //-------------------------------------------------------------------

    return (T)result;
}

public static T GetPlatformType<T>() where T : struct
{
    string platform = System.Configuration.ConfigurationManager.AppSettings["Platform"];
    T value;
    if (Enum.TryParse(platform, out value))
        return value;
    else
        return default(T);
}

我在编译时收到以下错误:

找不到类型或命名空间名称“platformType”(是否缺少 using 指令或程序集引用?)。

我该如何调用这个方法?

提前致谢。

I'm not experienced using reflection and generic methods, here are two methods. I think you can understand the thing I'm trying to do.

public static T GetInHeaderProperty<T>() where T : new()
{
    dynamic result = new T();

    result.CompanyId = ConfigurationManager.AppSettings["CompanyId"];
    result.UserId = ConfigurationManager.AppSettings["UserId"];
    result.Password = ConfigurationManager.AppSettings["Password"];
    result.MessageId = ConfigurationManager.AppSettings["MessageId"];

    Type platformType = typeof(T).GetProperty("PlatformType").PropertyType;
    // Here is my problem, I can not compile my code because of this line
    result.PlatformType = (dynamic)GetPlatformType<platformType>();
    //-------------------------------------------------------------------

    return (T)result;
}

public static T GetPlatformType<T>() where T : struct
{
    string platform = System.Configuration.ConfigurationManager.AppSettings["Platform"];
    T value;
    if (Enum.TryParse(platform, out value))
        return value;
    else
        return default(T);
}

I'm getting the following error at compile time:

The type or namespace name 'platformType' could not be found (are you missing a using directive or an assembly reference?).

How can I call this method?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

早乙女 2025-01-05 06:03:33

GetPlatformType 是一个泛型方法,但您不是向其传递泛型参数,而是向其传递描述类型的 Type 对象。泛型参数 T 必须在编译时已知,而不是在运行时传递。

您可以使用 Enum.Parse 重载,将 Type 对象传递给它,但您必须自己将其包装在 try/catch 块中(无 TryParse 重载)。

GetPlatformType is a generic method, but instead of passing it a generic parameter, you're passing it a Type object that describes the type. A generic parameter T must be known during compile time, not passed in runtime.

You can use the Enum.Parse overload, passing it the Type object, but you'll have to wrap it in a try/catch block yourself (no TryParse overload).

美人迟暮 2025-01-05 06:03:33

尝试使用 MakeGenericMethod

您需要首先获取该方法的MethodInfo。也许有更好的方法来使用一些动态的东西,但这是我通常采用的方法。最后你需要调用Invoke

Try using MakeGenericMethod.

You need to get the MethodInfo for the method first. Maybe there is a better way with using some dynamic stuff but this is the way i usually go. Finally you need to call Invoke.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文