C# 反射:为泛型方法提供 T
我没有使用反射和泛型方法的经验,这里有两种方法。我想你能理解我想做的事情。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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).
尝试使用 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.