Amazon.DynamoDBv2.DataModel.Utils.Instantiate C# (CSharp) 메소드

Instantiate() 공개 정적인 메소드

public static Instantiate ( Type objectType ) : object
objectType System.Type
리턴 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)

Usage Example

예제 #1
0
        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);
        }
All Usage Examples Of Amazon.DynamoDBv2.DataModel.Utils::Instantiate