在转换时,不允许对非通用环境错误进行约束

时间:2016-09-09 05:25:52

标签: c# xamarin casting

我正在尝试将Java lang对象转换为特定的类类型(Account)。

public static T Cast(Object obj) where T : class
{
    var propertyInfo = obj.GetType().GetProperty("Instance");
    return propertyInfo == null ? null : propertyInfo.GetValue(obj, null) as T;
}

抛出错误

  

非通用环境不允许使用约束。

过去两天我一直被困在这里。

1 个答案:

答案 0 :(得分:4)

您必须将泛型类型参数public static T Cast<T>(Object obj) where T : class { var propertyInfo = obj.GetType().GetProperty("Instance"); return propertyInfo == null ? null : propertyInfo.GetValue(obj, null) as T; } 添加到方法

{{1}}