/// <summary>转换单个对象</summary> /// <param name="obj"></param> /// <param name="interfaceType"></param> /// <returns></returns> public static Object Implement(Object obj, Type interfaceType) { if (obj == null) { throw new ArgumentNullException("obj"); } //Type duckedType = obj.GetType(); ValidateInterfaceType(interfaceType); Type duckType = GetDuckType(interfaceType, obj.GetType()); //return Activator.CreateInstance(duckType, obj); return(TypeX.CreateInstance(duckType, obj)); }
public override Object CreateInstance(params Object[] parameters) { if (Type.ContainsGenericParameters || Type.IsGenericTypeDefinition) { throw new XException(Type.FullName + "类是泛型定义类,缺少泛型参数!"); } if (Type.IsValueType || Type.IsArray) { return(Handler.Invoke(parameters)); } // 准备参数类型数组,以匹配构造函数 //var paramTypes = Type.EmptyTypes; //if (parameters != null && parameters.Length > 0) //{ // var list = new List<Type>(); // foreach (var item in parameters) // { // if (item != null) // list.Add(item.GetType()); // else // list.Add(typeof(Object)); // } // paramTypes = list.ToArray(); //} var paramTypes = TypeX.GetTypeArray(parameters); var ctor = GetConstructor(paramTypes); var handler = GetHandler(ctor); if (handler != null) { return(handler.Invoke(parameters)); } if (paramTypes != Type.EmptyTypes) { paramTypes = Type.EmptyTypes; ctor = GetConstructor(paramTypes); handler = GetHandler(ctor); if (handler != null) { // 更换了构造函数,要重新构造构造函数的参数 var ps = ctor.GetParameters(); parameters = new Object[ps.Length]; for (int i = 0; i < ps.Length; i++) { // 处理值类型 if (ps[i].ParameterType.IsValueType) { parameters[i] = TypeX.CreateInstance(ps[i].ParameterType); } else { parameters[i] = null; } } // 如果这里创建失败,后面还可以创建一个未初始化 try { return(handler.Invoke(parameters)); } catch { } } } // 如果没有找到构造函数,则创建一个未初始化的对象 return(FormatterServices.GetSafeUninitializedObject(Type)); }
/// <summary></summary> public static void Test() { XTrace.WriteLine("创建类型……"); #region TypeX类型 TypeX type = TypeX.Create(typeof(FastTest)); Object obj = type.CreateInstance(); Debug.Assert(obj != null, "创建实例出错!"); obj = type.CreateInstance(123); Debug.Assert(obj != null, "创建实例出错!"); //obj = type.CreateInstance("1234"); //Debug.Assert(obj != null, "创建实例出错!"); obj = type.CreateInstance(111, "aaa"); Debug.Assert(obj != null, "创建实例出错!"); XTrace.WriteLine("创建值类型实例"); type = TypeX.Create(typeof(ConsoleKeyInfo)); obj = type.CreateInstance(); Debug.Assert(obj != null, "创建值类型实例出错!"); XTrace.WriteLine("创建数组类型实例"); type = TypeX.Create(typeof(ConsoleKeyInfo[])); obj = type.CreateInstance(5); Debug.Assert(obj != null, "创建数组类型实例出错!"); #endregion #region 构造函数 ConstructorInfoX ctr = ConstructorInfoX.Create(typeof(FastTest)); obj = ctr.CreateInstance(); Debug.Assert(obj != null, "创建实例出错!"); ctr = ConstructorInfoX.Create(typeof(FastTest), new Type[] { typeof(Int32) }); obj = ctr.CreateInstance(123); Debug.Assert(obj != null, "创建实例出错!"); ctr = ConstructorInfoX.Create(typeof(FastTest), new Type[] { typeof(Int32), typeof(String) }); obj = ctr.CreateInstance(111, "aaa"); Debug.Assert(obj != null, "创建实例出错!"); #endregion #region 字段 FieldInfoX field = FieldInfoX.Create(typeof(FastTest), "_ID"); (obj as FastTest).ID = 111; Int32 v = (Int32)field.GetValue(obj); Debug.Assert(v == 111, "字段取值出错!"); field.SetValue(obj, 888); v = (Int32)field.GetValue(obj); Debug.Assert(v == 888, "字段赋值出错!"); KeyValuePair <Int32, Int32> kv = new KeyValuePair <int, int>(123456, 222); field = FieldInfoX.Create(kv.GetType(), "Key"); //field.SetValue(kv, 123456); v = (Int32)field.GetValue(kv); Debug.Assert(v == 123456, "字段取值出错!"); field = FieldInfoX.Create(typeof(FastTest), "_Name"); field.SetValue("动态赋值"); String v2 = (String)field.GetValue(); Debug.Assert(v2 == "动态赋值", "静态字段出错!"); #endregion #region 属性 PropertyInfoX p = typeof(FastTest).GetProperty("ID"); v = (Int32)p.GetValue(obj); Debug.Assert(v == 888, "属性取值出错!"); p.SetValue(obj, 999); v = (Int32)p.GetValue(obj); Debug.Assert(v == 999, "属性赋值出错!"); p = PropertyInfoX.Create(typeof(FastTest), "Name"); field.SetValue("属性动态赋值"); v2 = (String)field.GetValue(); Debug.Assert(v2 == "属性动态赋值", "静态字段出错!"); #endregion #region 方法 MethodInfoX method = MethodInfoX.Create(typeof(FastTest), "Test2"); method.Invoke(obj); method = typeof(FastTest).GetMethod("GetFullName"); Console.WriteLine(method.Invoke(null, 123, "abc")); #endregion }
public override Object CreateInstance(Type type, params Object[] parameters) { return(TypeX.CreateInstance(type, parameters)); }
public override Object CreateInstance(params Object[] parameters) { if (Type.ContainsGenericParameters || Type.IsGenericTypeDefinition) { throw new XException(Type.FullName + "类是泛型定义类,缺少泛型参数!"); } if (Type.IsValueType) { return(Handler.Invoke(parameters)); } // 数组的动态构造参数是元素个数,如果未指定,应该默认0 if (Type.IsArray) { if (parameters == null || parameters.Length < 1) { parameters = new Object[] { 0 } } ; return(Handler.Invoke(parameters)); } //// 无参数,直接构造 //if (parameters == null || parameters.Length < 1) return Handler.Invoke(new Object[0]); // 准备参数类型数组,以匹配构造函数 //var paramTypes = Type.EmptyTypes; //if (parameters != null && parameters.Length > 0) //{ // var list = new List<Type>(); // foreach (var item in parameters) // { // if (item != null) // list.Add(item.GetType()); // else // list.Add(typeof(Object)); // } // paramTypes = list.ToArray(); //} var paramTypes = TypeX.GetTypeArray(parameters); var ctor = GetConstructor(paramTypes); var handler = GetHandler(ctor); if (handler != null) { return(handler.Invoke(parameters)); } if (paramTypes != Type.EmptyTypes) { paramTypes = Type.EmptyTypes; ctor = GetConstructor(paramTypes); handler = GetHandler(ctor); if (handler != null) { // 更换了构造函数,要重新构造构造函数的参数 var ps = ctor.GetParameters(); parameters = new Object[ps.Length]; for (int i = 0; i < ps.Length; i++) { // 处理值类型 if (ps[i].ParameterType.IsValueType) { parameters[i] = TypeX.CreateInstance(ps[i].ParameterType); } else { parameters[i] = null; } } // 如果这里创建失败,后面还可以创建一个未初始化 try { return(handler.Invoke(parameters)); } catch { } } } // 如果没有找到构造函数,则创建一个未初始化的对象 return(FormatterServices.GetSafeUninitializedObject(Type)); } DictionaryCache <ConstructorInfo, FastHandler> _cache = new DictionaryCache <ConstructorInfo, FastHandler>(); FastHandler GetHandler(ConstructorInfo constructor) { if (constructor == null) { return(null); } return(_cache.GetItem(constructor, key => GetConstructorInvoker(Type, key))); } ConstructorInfo GetConstructor(Type[] paramTypes) { var bf = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; // 1,如果参数为null,则找第一个参数 // 2,根据参数找一次,如果参数为空,也许能找到无参构造函数 // 3,如果还没找到,参数又为空,采用第一个构造函数。这里之所以没有和第一步合并,主要是可能调用者只想要无参构造函数,而第一个不是 ConstructorInfo constructor = null; if (paramTypes == null) { constructor = Type.GetConstructors(bf).FirstOrDefault(); paramTypes = Type.EmptyTypes; } if (constructor == null) { constructor = Type.GetConstructor(bf, null, paramTypes, null); } //if (constructor == null) throw new Exception("没有找到匹配的构造函数!"); if (constructor == null && paramTypes == Type.EmptyTypes) { constructor = Type.GetConstructors(bf).FirstOrDefault(); } return(constructor); }