예제 #1
0
        public static TType Construct <TType>(Type derivedType)
        {
            if (typeof(TType).IsAssignableFrom(derivedType))
            {
                return(TypeConstructionCache <TType> .Construct(derivedType));
            }

            throw new ArgumentException($"Could not create instance of type `{derivedType.Name}` and convert to `{typeof(TType).Name}`: given type is not assignable to target type.");
        }
예제 #2
0
        /// <summary>
        /// Sets the explicit construction method for the <see cref="TType"/>.
        /// </summary>
        /// <param name="constructor">The construction method.</param>
        /// <typeparam name="TType">The type to set the explicit construction method.</typeparam>
        /// <returns><see langword="true"/> if the constructor was set; otherwise, <see langword="false"/>.</returns>
        public static bool TrySetExplicitConstructionMethod <TType>(ConstructorMethod <TType> constructor)
        {
            if (TypeConstructionCache.HasExplicitConstruction <TType>())
            {
                return(false);
            }

            TypeConstructionCache.SetExplicitConstruction(constructor);
            return(true);
        }
예제 #3
0
        public static bool TryConstruct <TType>(Type derivedType, out TType value)
        {
            if (typeof(TType).IsAssignableFrom(derivedType))
            {
                return(TypeConstructionCache <TType> .TryConstruct(derivedType, out value));
            }

            value = default;
            return(false);
        }
예제 #4
0
        /// <summary>
        /// Tries to constructs a new instance of the given type type and returns it as <see cref="TType"/>.
        /// </summary>
        /// <param name="derivedType">The type we want to create a new instance of.</param>
        /// <param name="value">When this method returns, contains the created instance, if type construction succeeded; otherwise, the default value for <typeparamref name="TType"/>.</param>
        /// <typeparam name="TType">The type we want to create a new instance of.</typeparam>
        /// <returns><see langword="true"/> if a new instance of the given type could be created.</returns>
        public static bool TryConstruct <TType>(Type derivedType, out TType value)
        {
            if (TypeConstructionCache.TryConstruct(derivedType, out value))
            {
                return(true);
            }

            value = default;
            return(false);
        }
예제 #5
0
        /// <summary>
        /// Un-sets the explicit construction method for the <see cref="TType"/> type.
        /// </summary>
        /// <remarks>
        /// An explicit construction method can only be unset if it was previously set with the same instance.
        /// </remarks>
        /// <param name="constructor">The construction method.</param>
        /// <typeparam name="TType">The type to set the explicit construction method.</typeparam>
        /// <returns><see langword="true"/> if the constructor was unset; otherwise, <see langword="false"/>.</returns>
        public static bool TryUnsetExplicitConstructionMethod <TType>(ConstructorMethod <TType> constructor)
        {
            if (TypeConstructionCache.GetExplicitConstruction <TType>() != constructor)
            {
                return(false);
            }

            TypeConstructionCache.SetExplicitConstruction <TType>(null);
            return(true);
        }
예제 #6
0
 /// <summary>
 /// Returns <see langword="true"/> if type <see cref="T"/> is constructable from any of its derived types.
 /// </summary>
 /// <remarks>
 /// Constructable is defined as either having a default or implicit constructor or having a registered construction method.
 /// </remarks>
 /// <typeparam name="T">The type to query.</typeparam>
 /// <returns><see langword="true"/> if type <see cref="T"/> is constructable from any of its derived types.</returns>
 public static bool CanBeConstructedFromDerivedType <T>()
 {
     return(TypeConstructionCache.GetConstructableTypes <T>().Any(type => type != typeof(T)));
 }
예제 #7
0
 /// <summary>
 /// Constructs a new instance of the specified <see cref="TType"/>.
 /// </summary>
 /// <typeparam name="TType">The type we want to create a new instance of.</typeparam>
 /// <returns>A new instance of the <see cref="TType"/>.</returns>
 public static TType Construct <TType>()
 {
     return(TypeConstructionCache.Construct <TType>());
 }
예제 #8
0
 /// <summary>
 /// Constructs a new instance of the specified <see cref="TType"/>.
 /// </summary>
 /// <param name="instance">When this method returns, contains the created instance, if type construction succeeded; otherwise, the default value for <typeparamref name="TType"/>.</param>
 /// <typeparam name="TType">The type to create an instance of.</typeparam>
 /// <returns><see langword="true"/> if a new instance of type <see cref="TType"/> was created; otherwise, <see langword="false"/>.</returns>
 public static bool TryConstruct <TType>(out TType instance)
 {
     return(TypeConstructionCache.TryConstruct(out instance));
 }
예제 #9
0
 /// <summary>
 /// Returns <see langword="true"/> if type <see cref="T"/> is constructable.
 /// </summary>
 /// <remarks>
 /// Constructable is defined as either having a default or implicit constructor or having a registered construction method.
 /// </remarks>
 /// <typeparam name="T">The type to query.</typeparam>
 /// <returns><see langword="true"/> if type <see cref="T"/> is constructable.</returns>
 public static bool CanBeConstructed <T>()
 {
     return(TypeConstructionCache.CanBeConstructed <T>());
 }
예제 #10
0
 /// <summary>
 /// Returns <see langword="true"/> if the specified type is constructable.
 /// </summary>
 /// <remarks>
 /// Constructable is defined as either having a default or implicit constructor or having a registered construction method.
 /// </remarks>
 /// <param name="type">The type to query.</param>
 /// <returns><see langword="true"/> if the given type is constructable.</returns>
 public static bool CanBeConstructed(Type type)
 {
     return(TypeConstructionCache.CanBeConstructed(type));
 }
예제 #11
0
 static void RegisterBuiltInConstructors()
 {
     TypeConstructionCache.SetExplicitConstruction(() => string.Empty);
 }
예제 #12
0
 /// <summary>
 /// Constructs a new instance of the given type type and returns it as <see cref="TType"/>.
 /// </summary>
 /// <param name="derivedType">The type we want to create a new instance of.</param>
 /// <typeparam name="TType">The type we want to create a new instance of.</typeparam>
 /// <returns>a new instance of the <see cref="TType"/> type.</returns>
 /// <exception cref="ArgumentException">Thrown when the given type is not assignable to <see cref="TType"/></exception>
 public static TType Construct <TType>(Type derivedType)
 {
     return(TypeConstructionCache.Construct <TType>(derivedType));
 }
예제 #13
0
 public static bool CanBeConstructedFromDerivedType <T>()
 {
     return(TypeConstructionCache <T> .ConstructibleTypes.Count >
            (TypeConstructionCache <T> .CanBeConstructed() ? 1 : 0));
 }
예제 #14
0
 /// <summary>
 /// Adds all the constructable types from the provided type to the given list.
 /// </summary>
 /// <param name="type">The type to query.</param>
 /// <param name="result">List to contain the results.</param>
 public static void GetAllConstructableTypes(Type type, List <Type> result)
 {
     result.AddRange(TypeConstructionCache.GetConstructableTypes(type));
 }
예제 #15
0
 /// <summary>
 /// Returns a list of all the constructable types from the provided type.
 /// </summary>
 /// /// <param name="type">The type to query.</param>
 /// <returns>A list of all the constructable types from the provided type.</returns>
 public static IEnumerable <Type> GetAllConstructableTypes(Type type)
 {
     return(TypeConstructionCache.GetConstructableTypes(type));
 }
예제 #16
0
 /// <summary>
 /// Adds all the constructable types from the <see cref="TType"/> type to the given list.
 /// </summary>
 /// <param name="result">List to contain the results.</param>
 /// <typeparam name="TType">The type to query.</typeparam>
 public static void GetAllConstructableTypes <TType>(List <Type> result)
 {
     result.AddRange(TypeConstructionCache.GetConstructableTypes <TType>());
 }
예제 #17
0
 public static bool TryConstruct <TType>(out TType value)
 {
     return(TypeConstructionCache <TType> .TryConstruct(out value));
 }