public static Instantiate ( |
||
objectType | ||
return | object |
public static object Instantiate(Type objectType)
{
if (objectType == null)
throw new ArgumentNullException("objectType");
if (!CanInstantiate(objectType))
throw new InvalidOperationException("Cannot instantiate type " + objectType.FullName);
var objectTypeWrapper = TypeFactory.GetTypeInfo(objectType);
var constructorInfo = objectTypeWrapper.GetConstructor(TypeFactory.EmptyTypes);
if (constructorInfo == null) throw new InvalidOperationException("Type must have a parameterless constructor");
object instance = constructorInfo.Invoke(null);
return instance;
}
public static bool CanInstantiate(Type objectType)
public static bool ItemsToCollection(Type targetType, IEnumerable <object> items, out object result) { result = Utils.Instantiate(targetType); var ilist = result as IList; if (ilist != null) { foreach (var item in items) { ilist.Add(item); } return(true); } var targetTypeInfo = TypeFactory.GetTypeInfo(targetType); var addMethod = targetTypeInfo.GetMethod("Add"); if (addMethod != null) { foreach (var item in items) { addMethod.Invoke(result, new object[] { item }); } return(true); } result = null; return(false); }