20
我在.net 4.6.2此代碼,現在正試圖轉換成淨核心不過我收到錯誤.Net核心中缺少IsGenericType&IsValueType?
錯誤CS1061「類型」不包含「IsGenericType」 並沒有擴展名的定義方法「IsGenericType」接受 類型「類型」的第一個參數可以找到(是否缺少using指令或一個 集引用?)
public static class StringExtensions
{
public static TDest ConvertStringTo<TDest>(this string src)
{
if (src == null)
{
return default(TDest);
}
return ChangeType<TDest>(src);
}
private static T ChangeType<T>(string value)
{
var t = typeof(T);
// getting error here at t.IsGenericType
if (t.IsGenericType && t.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
{
if (value == null)
{
return default(T);
}
t = Nullable.GetUnderlyingType(t);
}
return (T)Convert.ChangeType(value, t);
}
}
什麼是在.net核心等效?
UPDATE1
令人驚訝,當我調試的代碼,我看到變量t
具有IsGenericType
財產,但是我不能在代碼中使用IsGenericType
。不知道爲什麼或者我需要添加哪個命名空間。我已經加入using System
和using System.Runtime
兩個命名空間
@svick在某些其他命名空間中是否使用GetTypeInfo()擴展方法? intelisense找不到它 – LP13
不知道你爲什麼問我,但是,它在'System.Reflection'中。 – svick