コード例 #1
0
        public override JsonContract ResolveContract(Type type)
		{
			var contract = base.ResolveContract(type);
		
			if (contract is JsonObjectContract && 
                !type.IsValueType &&
                !type.HasDefaultConstructor())
			{
				var defaultCreator = contract.DefaultCreator;
				contract.DefaultCreator = () =>
				                          	{
				                          		try
				                          		{
                                                    // Todo: Structs without default constructor will fail with this and that will then try using the defaultCreator in the catch
				                          			return _container.Get(type);
				                          		}
				                          		catch
				                          		{
				                          			return defaultCreator();
				                          		}
				                          	};
			}

			return contract;
        }
コード例 #2
0
        public static Delegate Creator(Type t)
        {            
            if (t == typeof(string))
                return Expression.Lambda(Expression.Constant(string.Empty)).Compile();

            if (t.HasDefaultConstructor())
                return Expression.Lambda(Expression.New(t)).Compile();

            return null;
        }
コード例 #3
0
        /// <summary>
        /// Creates an instance of the given type using the default constructor.
        /// </summary>
        /// <param name="type">The type to create.</param>
        /// <returns>An instance of the type.</returns>
        /// <exception cref="NoTypesFoundException">
        /// If no implementing types of <paramref name="type"/> was found.
        /// </exception>
        /// <exception cref="MissingDefaultConstructorException">
        /// If the found type does not have a default constructor.
        /// </exception>
        public object Create(Type type)
        {
            if (!type.IsImplementation())
            {
                throw new NoTypesFoundException(type);
            }

            if (!type.HasDefaultConstructor())
            {
                throw new MissingDefaultConstructorException(type);
            }

            return Activator.CreateInstance(type);
        }
コード例 #4
0
ファイル: DataToObjects.cs プロジェクト: ZhenghaiYe/iveely
        public DataToObjects(Type type, Func<Type, MemberInfo, int> membersOrder = null)
        {
            if (!DataType.IsPrimitiveType(type) && !type.HasDefaultConstructor())
                throw new NotSupportedException("No default constructor.");

            bool isSupported = DataTypeUtils.IsAllPrimitive(type);
            if (!isSupported)
                throw new NotSupportedException("Not all types are primitive.");

            Type = type;
            MembersOrder = membersOrder;

            LambdaFromObjects = CreateFromObjectsMethod();
            fromObjects = LambdaFromObjects.Compile();

            LambdaToObjects = CreateToObjectsMethod();
            toObjects = LambdaToObjects.Compile();
        }
コード例 #5
0
ファイル: Configure.cs プロジェクト: jarlef/Bifrost
 static void ThrowIfCanCreateContainerDoesNotHaveDefaultConstructor(Type createContainerType)
 {
     if (!createContainerType.HasDefaultConstructor())
         throw new MissingDefaultConstructorException(createContainerType);
 }
コード例 #6
0
 public bool IsInstantiatable(Type type, Type targetType)
 {
     return !type.IsAbstract &&
      !type.IsGenericType &&
      type.HasDefaultConstructor() &&
      targetType.IsAssignableFrom(type);
 }
コード例 #7
0
 public void AllSettingsHaveDefaultCtor(Type settingType)
 {
     Assert.True(settingType.HasDefaultConstructor(),
         string.Format("{0} should have default ctor.", settingType.Name));
 }
コード例 #8
0
 private static bool RegistryHasDefaultConstructor(Type type)
 {
     return type.HasDefaultConstructor();
 }
コード例 #9
0
 private bool IsValidDestinationType(Type destinationType)
 {
     return (destinationType.IsArray || destinationType.HasDefaultConstructor());
 }