Vài code thông não

By
Advertisement

----------Create new object from type ------------------
ConstructorInfo itemConstructor;
Type itemType = obj.GetType();

itemConstructor = itemType.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.CreateInstance, null, new Type[0], null);

object newObj = itemConstructor.Invoke(null);

---------- Create Bindding List Generic -----------------
Type typeFromHandle = typeof(BindingList<>);
Type type2 = typeFromHandle.MakeGenericType(new Type[]
{
typeof(string)
});

BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.CreateInstance;
object[] args = null;

Activator.CreateInstance(type2, bindingFlags, null, args, null);

To be Continue...

0 blogger:

Đăng nhận xét