public void CreateInstance_ClassWithInvalidFactoryNoParameterless_Exception(ReflectionsUtilsStrategy strategy) { Type type = typeof(ClassWithInvalidFactoryNoParameterless); string friendlyName = type.GetFriendlyName(); var ex = Assert.Throws <InvalidMethodException>(() => CreateInstanceAndTest(strategy, type)); ex.AssertArguments("Static", "Create", friendlyName, 0, friendlyName, 1, friendlyName); }
public void CreateReturnlessOneParameterMethod_InvalidMethod_Exception(ReflectionsUtilsStrategy strategy) { Type type = typeof(ClassWithReturnlessOneParameterMethods <>).MakeGenericType(typeof(int)); string friendlyName = type.GetFriendlyName(); string voidTypeName = typeof(void).GetFriendlyName(); var ex = Assert.Throws <InvalidMethodException>(() => ReflectionUtils.CreateReturnlessOneParameterMethodCallback(type, type.GetMethod(InvalidMethodName))); ex.AssertArguments("Static", InvalidMethodName, friendlyName, 1, voidTypeName, 0, voidTypeName); }
private static bool TrySetStrategy(ReflectionsUtilsStrategy strategy) { if (!ReflectionUtils.SetStrategy(strategy)) { Assert.Ignore("{0} strategy not supported on the target environment.", strategy); return(false); } else { return(true); } }
/// <summary> /// Sets a new strategy <see cref="ReflectionUtils"></see>. /// </summary> /// <param name="newStrategy"> New strategy. </param> /// <returns> True if strategy has been changed, otherwise false. </returns> /// <remarks> /// Should be raised only for testing purposes only. /// The better strategy for the target environment will be set automatically. /// </remarks> internal static bool SetStrategy(ReflectionsUtilsStrategy newStrategy) { if (isCodeGenerationIsSupported) { strategy = newStrategy; return(true); } else { return(false); } }
private static void CreateMDArrayAndTest(ReflectionsUtilsStrategy strategy, Type elementType, int rank) { if (!TrySetStrategy(strategy)) { return; } IFunctionCallback <int[], IEnumerable> mdArrayActivator = ReflectionUtils.CreateMDArray(elementType, rank); Array array = mdArrayActivator.Invoke(new int[rank].Select((v, i) => 1)) as Array; Assert.AreEqual(rank, array.Rank); Assert.AreEqual(1, array.Length); }
private static void CreateDictionaryKVAndTest(ReflectionsUtilsStrategy strategy, Type dictionaryType, params int[] args) { if (!TrySetStrategy(strategy)) { return; } IFunctionCallback <int[], IEnumerable> dictionaryActivator = ReflectionUtils.CreateDictionary(dictionaryType.GetGenericTypeDefinition(), dictionaryType.GetGenericArguments()[0], dictionaryType.GetGenericArguments()[1], args.Length); IDictionary dictionary = dictionaryActivator.Invoke(args) as IDictionary; Assert.NotNull(dictionary); Assert.AreEqual(0, dictionary.Cast <object>().Count()); }
private static void CreateEnumerableTAndTest(ReflectionsUtilsStrategy strategy, Type collectionType, params int[] args) { if (!TrySetStrategy(strategy)) { return; } IFunctionCallback <int[], IEnumerable> enumerableActivator = ReflectionUtils.CreateEnumerable(collectionType.GetGenericTypeDefinition(), collectionType.GetGenericArguments()[0], args.Length); IEnumerable enumerable = enumerableActivator.Invoke(args) as IEnumerable; Assert.NotNull(enumerable); Assert.AreEqual(0, enumerable.Cast <object>().Count()); }
/// <summary> /// Initializes the <see cref="ReflectionUtils"></see>. /// </summary> static ReflectionUtils() { // Determines whether code generation is supported or not. try { DynamicMethod method = new DynamicMethod(string.Empty, objectType, Type.EmptyTypes, true); // Code generation is supported (dynamic methods may be used) isCodeGenerationIsSupported = true; strategy = ReflectionsUtilsStrategy.Auto; } catch { // Code generation isn't supported (reflection should be used) isCodeGenerationIsSupported = false; strategy = ReflectionsUtilsStrategy.UseOnlyReflection; } }
private static void CreateAndInvokeReturnOneParameterMethod(ReflectionsUtilsStrategy strategy, bool isStaticMethod, Type type, object value) { if (!TrySetStrategy(strategy)) { return; } Type classType = typeof(ClassWithReturnOneParameterMethods <>).MakeGenericType(type); if (isStaticMethod) { object staticValue = InvokeReturnOneParameterMethod(classType, null, StaticMethodName, value); Assert.AreEqual(value, staticValue); } else { object instance = ReflectionUtils.CreateInstance(classType).Invoke(); object instanceValue = InvokeReturnOneParameterMethod(classType, instance, InstanceMethodName, value); Assert.AreEqual(value, instanceValue); } }
private static void CreateInstanceAndTest(ReflectionsUtilsStrategy strategy, Type type) { if (!TrySetStrategy(strategy)) { return; } IFunctionCallback <object> instanceActivator = ReflectionUtils.CreateInstance(type); IReflectionUtilsType instance = instanceActivator.Invoke() as IReflectionUtilsType; Assert.NotNull(instance); if (type.GetMethods(factoryMethodBindingFlags).FindOrDefault(m => m.GetAttribute <FactoryAttribute>(false) != null) != null) { Assert.True(instance.CreatedFromFactory); } else { Assert.False(instance.CreatedFromFactory); } }
private static void CreateAndInvokeSetAccessorProperty(ReflectionsUtilsStrategy strategy, bool isStaticProperty, Type type, object obj) { if (!TrySetStrategy(strategy)) { return; } Type classType = typeof(ClassWithProperties <>).MakeGenericType(type); if (isStaticProperty) { SetPropertyValue(classType, null, StaticPropertyName, obj); Assert.AreEqual(obj, classType.GetProperty(StaticPropertyName).GetValue(null)); } else { object instance = ReflectionUtils.CreateInstance(classType).Invoke(); SetPropertyValue(classType, instance, InstancePropertyName, obj); Assert.AreEqual(obj, classType.GetProperty(InstancePropertyName).GetValue(instance)); } }
private static void CreateAndInvokeGetAccessorField(ReflectionsUtilsStrategy strategy, bool isStaticField, Type type, object obj) { if (!TrySetStrategy(strategy)) { return; } Type classType = typeof(ClassWithFields <>).MakeGenericType(type); if (isStaticField) { classType.GetField(StaticFieldName).SetValue(null, obj); Assert.AreEqual(obj, GetFieldValue(classType, null, StaticFieldName)); } else { object instance = ReflectionUtils.CreateInstance(classType).Invoke(); classType.GetField(InstanceFieldName).SetValue(instance, obj); Assert.AreEqual(obj, GetFieldValue(classType, instance, InstanceFieldName)); } }
public void CreateReturnParameterlessMethod_InstanceMethodObjectType_Value(Type type, object value, ReflectionsUtilsStrategy strategy) { CreateAndInvokeReturnParameterlessMethod(strategy, false, typeof(object), value); }
public void CreateReturnParameterlessMethod_StaticMethodValueType_value(Type type, object value, ReflectionsUtilsStrategy strategy) { CreateAndInvokeReturnParameterlessMethod(strategy, true, type, value); }
public void CreateMDArray_AllTypesRank14_ArrayRank14(Type type, object obj, ReflectionsUtilsStrategy strategy) { CreateMDArrayAndTest(strategy, type, 14); }
public void CreateReturnlessOneParameterMethod_InstanceMethodValueType(Type type, object value, ReflectionsUtilsStrategy strategy) { CreateAndInvokeReturnlessOneParameterMethod(strategy, false, type, value); }
public void CreateInstance_StructWithInvalidFactoryInvalidReturnType_Exception(ReflectionsUtilsStrategy strategy) { Type type = typeof(StructWithInvalidFactoryInvalidReturnType); string friendlyName = type.GetFriendlyName(); var ex = Assert.Throws <InvalidMethodException>(() => CreateInstanceAndTest(strategy, type)); ex.AssertArguments("Static", "Create", friendlyName, 0, friendlyName, 0, typeof(object).GetFriendlyName()); }
public void CreateSetAccessorProperty_InstancePropertyValueType_Value(Type type, object obj, ReflectionsUtilsStrategy strategy) { CreateAndInvokeSetAccessorProperty(strategy, false, type, obj); }
public void CreateInstance_ClassWithInvalidFactoryMoreThanOneFactory_Exception(ReflectionsUtilsStrategy strategy) { Type type = typeof(ClassWithInvalidFactoryMoreThanOneFactory); string friendlyName = type.GetFriendlyName(); var ex = Assert.Throws <DuplicateFactoryMethodException>(() => CreateInstanceAndTest(strategy, type)); ex.AssertArguments("Create2", friendlyName); }
public void CreateSetAccessorField_StaticFieldValueType_Value(Type type, object obj, ReflectionsUtilsStrategy strategy) { CreateAndInvokeSetAccessorField(strategy, true, type, obj); }
public void CreateInstance_ClassWithoutFactoryOrParameterlessConstructor_Exception(ReflectionsUtilsStrategy strategy) { Type type = typeof(ClassWithoutFactoryOrParameterlessConstructor); string friendlyName = type.GetFriendlyName(); var ex = Assert.Throws <ConstructorNotFoundException>(() => CreateInstanceAndTest(strategy, type)); ex.AssertArguments(0, friendlyName); }
public void CreateSetAccessorProperty_StaticPropertyValueType_Value(Type type, object obj, ReflectionsUtilsStrategy strategy) { CreateAndInvokeSetAccessorProperty(strategy, true, type, obj); }
public void CreateReturnOneParameterMethod_StaticMethodValueType_Object(Type type, object value, ReflectionsUtilsStrategy strategy) { CreateAndInvokeReturnOneParameterMethod(strategy, true, typeof(object), value); }
public void CreateSetAccessorField_InstanceFieldValueType_Value(Type type, object obj, ReflectionsUtilsStrategy strategy) { CreateAndInvokeSetAccessorField(strategy, false, type, obj); }
public void CreateInstance_NotPrimitiveType_Instance(Type type, object obj, ReflectionsUtilsStrategy strategy) { CreateInstanceAndTest(strategy, type); }
public void CreateDictonary_AllTypesDictonaryOneArg_Dictionary(Type type, object obj, ReflectionsUtilsStrategy strategy) { CreateDictionaryKVAndTest(strategy, typeof(SortedList <,>).MakeGenericType(type, type), 5); }
public void CreateDictonary_AllTypesDictonaryTwoArg_Dictionary(Type type, object obj, ReflectionsUtilsStrategy strategy) { CreateDictionaryKVAndTest(strategy, typeof(ConcurrentDictionary <,>).MakeGenericType(type, type), 4 * Environment.ProcessorCount, 5); }
public void CreateEnumerable_AllTypesEnumerableOneArg_Collection(Type type, object obj, ReflectionsUtilsStrategy strategy) { CreateEnumerableTAndTest(strategy, typeof(List <>).MakeGenericType(type), 5); }
public void CreateArray_AllTypes_Array(Type type, object obj, ReflectionsUtilsStrategy strategy) { CreateMDArrayAndTest(strategy, type, 1); }