Ejemplo n.º 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;
        }
Ejemplo n.º 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;
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 4
0
        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();
        }
Ejemplo n.º 5
0
 static void ThrowIfCanCreateContainerDoesNotHaveDefaultConstructor(Type createContainerType)
 {
     if (!createContainerType.HasDefaultConstructor())
         throw new MissingDefaultConstructorException(createContainerType);
 }
Ejemplo n.º 6
0
 public bool IsInstantiatable(Type type, Type targetType)
 {
     return !type.IsAbstract &&
      !type.IsGenericType &&
      type.HasDefaultConstructor() &&
      targetType.IsAssignableFrom(type);
 }
Ejemplo n.º 7
0
 public void AllSettingsHaveDefaultCtor(Type settingType)
 {
     Assert.True(settingType.HasDefaultConstructor(),
         string.Format("{0} should have default ctor.", settingType.Name));
 }
 private static bool RegistryHasDefaultConstructor(Type type)
 {
     return type.HasDefaultConstructor();
 }
 private bool IsValidDestinationType(Type destinationType)
 {
     return (destinationType.IsArray || destinationType.HasDefaultConstructor());
 }