【发布时间】:2014-10-24 21:59:08
【问题描述】:
我有以下情况:
public class ListingAttribute : AgencyServicesApi.Attribute
{
public Type ClrType { get; set; }
//public void SetValue(object value)
//{
// this.SetValueGen<ClrType>((ClrType)value);
//}
public void SetValueGen<TValue>(TValue value)
{
var t = typeof(TValue);
Value = // Use conversion methods based on ClrType here.
}
}
我不能使ListingAttribute 泛型并为ClrType 使用泛型参数T,因为我必须在运行时设置类型。 AgencyServicesApi.Attribute 有一个类型为string 的臭Value 属性,不管Value 的内容是否应该是任何其他类型。我正在尝试扩展AgencyServicesApi.Attribute,并根据配置文件,正确设置ClrType,并能够添加正确的验证和ToString 实现。
如何调用泛型方法SetValueGen 并将ClrType 的类型作为类型参数传递给它? SetValue 被注释掉了,因为它不能编译,我不希望它编译,但我被这件事困住了。
【问题讨论】: