Type对象获得泛型类型的两个扩展方法
生活随笔
收集整理的這篇文章主要介紹了
Type对象获得泛型类型的两个扩展方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、定義擴展對象
1: public static class ExtendMethod 2: { 3: ? 4: public static Type GetSingleGenericType(this Type t) 5: { 6: Type[] ts = GetGenericType(t); 7: if (ts == null) return null; 8: return ts[0]; 9: } 10: ? 11: public static Type[] GetGenericType(this Type t) 12: { 13: if (!t.IsGenericType) return null; 14: List<Type> lt = new List<Type>(); 15: int begin = t.FullName.IndexOf('['); 16: int end = t.FullName.LastIndexOf(']'); 17: string str = t.FullName.Substring(begin + 1, end - begin - 1); 18: while(true) 19: { 20: begin = str.IndexOf('['); 21: if (begin < 0) break; 22: lt.Add(Type.GetType(str.Substring(begin + 1, str.IndexOf(',') - 1))); 23: str = str.Remove(0, str.IndexOf(']') + 1); 24: str = str.TrimStart(','); 25: } 26: return lt.ToArray(); 27: }2、應用示例。
1: Type type = typeof(Dictionary<int, string>); 2: Type[] tS = type.GetGenericType();這時tS中分別的System.Int32和System.String的Type對象。
轉載于:https://www.cnblogs.com/gleamy_ming/articles/2012671.html
總結
以上是生活随笔為你收集整理的Type对象获得泛型类型的两个扩展方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于域帐户将计算机加入域登陆上限问题
- 下一篇: Javascript自由拖拽类