T instance = default(T);
Type type = typeof(T);
System.Reflection.ConstructorInfo[] constructorInfoArray = type.GetConstructors(System.Reflection.BindingFlags.Instance
| System.Reflection.BindingFlags.NonPublic
| System.Reflection.BindingFlags.Public);
System.Reflection.ConstructorInfo noParameterConstructorInfo = null;
foreach (System.Reflection.ConstructorInfo constructorInfo in constructorInfoArray)
{
System.Reflection.ParameterInfo[] parameterInfoArray = constructorInfo.GetParameters();
if (0 == parameterInfoArray.Length)
{
noParameterConstructorInfo = constructorInfo;
break;
}
}
if (null == noParameterConstructorInfo)
{
throw new NotSupportedException("No constructor without 0 parameter");
}
instance = (T)noParameterConstructorInfo.Invoke(null);
反射的另外一种方法
最新推荐文章于 2023-06-14 09:18:38 发布