/// <summary> /// /// </summary> /// <param name="this"></param> /// <param name="descriptor"></param> /// <param name="arguments"></param> /// <returns></returns> public static object CreateInstanceImpl( [NotNull] this IntrospectiveStaticContext @this, [NotNull] MemberDescriptor descriptor, [ItemCanBeNull] params object[] arguments) { @this.IsNotNull(nameof(@this)); descriptor.IsNotNull(nameof(descriptor)); var parameterTypes = arguments .Select(t => t?.GetType()) .ToArray(); var constructorInfo = @this.TargetType.GetConstructor( descriptor, null, CallingConventions.Any, parameterTypes, new ParameterModifier[] { }); if (constructorInfo == null) { throw new TypeInitializationException(@this.TargetType.FullName, null); } var instance = constructorInfo.Invoke(arguments); return(instance); }
public static object CreateInstance( [NotNull] this IntrospectiveStaticContext @this, [NotNull] MemberDescriptor memberDescriptor, [ItemCanBeNull] params object[] arguments) { @this.IsNotNull(nameof(@this)); memberDescriptor.IsNotNull(nameof(memberDescriptor)); var parameterTypes = arguments .Select(t => t?.GetType()) .ToArray(); var constructorInfo = @this.TargetType.GetConstructor( memberDescriptor, null, CallingConventions.Any, parameterTypes, new ParameterModifier[] { }); if (constructorInfo == null) { throw new TypeInitializationException( @this.TargetType.FullName, null); } var instance = constructorInfo.Invoke(arguments); return(instance); //throw new InvalidCastException($"Cannot cast property value \'{returnVal.GetType().Name}\' to {typeof(TResult).Name}"); }
public static TReturns InvokeMethod <TReturns>( [NotNull] this IntrospectiveContext @this, [NotNull] MemberDescriptor memberDescriptor, [NotNull] string methodName, params object[] methodArgs) { @this.IsNotNull(nameof(@this)); memberDescriptor.IsNotNull(nameof(memberDescriptor)); methodName.IsNotNull(nameof(methodName)); var ownerType = @this.TargetType; var methodInfo = ownerType.GetMethod(methodName, memberDescriptor); if (methodInfo == null) { throw new MissingMethodException( ownerType.Name, methodName); } var rawReturnValue = methodInfo.Invoke( @this.TargetObject, methodArgs); if (rawReturnValue is TReturns) { return((TReturns)rawReturnValue); } throw new InvalidCastException( $"Cannot cast raw returned value of type \'{rawReturnValue.GetType().FormatName()}\' " + $"to the expected return type of \'{typeof(TReturns).FormatName()}\'."); }
public static TValue GetFieldValue <TValue>( [NotNull] this IntrospectiveContext @this, [NotNull] MemberDescriptor memberDescriptor, [NotNull] string fieldName) { @this.IsNotNull(nameof(@this)); memberDescriptor.IsNotNull(nameof(memberDescriptor)); fieldName.IsNotNull(nameof(fieldName)); var ownerType = @this.TargetType; var fieldInfo = ownerType.GetField(fieldName, memberDescriptor); if (fieldInfo == null) { throw new MissingMemberException(ownerType.Name, nameof(fieldName)); } var fieldType = fieldInfo.FieldType; var valueType = typeof(TValue); var rawValue = fieldInfo.GetValue(@this.TargetObject); return((TValue)rawValue); //if (!fieldType.IsAssignableFrom(valueType)) //var adjustedValue = value; //throw new InvalidCastException($"Cannot cast property value \'{returnVal.GetType().Name}\' to {typeof(TResult).Name}"); }
public static object GetFieldValue( [NotNull] this IntrospectiveContext @this, [NotNull] MemberDescriptor memberDescriptor, [NotNull] string fieldName) { return(@this.GetFieldValue <object>( memberDescriptor, fieldName)); }
public static IEnumerable <PropertyInfo> GetPropertiesOfType <TPropertyType>( [NotNull] this IntrospectiveStaticContext @this, [NotNull] MemberDescriptor memberDescriptor) { @this.IsNotNull(nameof(@this)); memberDescriptor.IsNotNull(nameof(memberDescriptor)); return(@this .GetProperties(memberDescriptor) .Where(t => t.PropertyType == typeof(TPropertyType))); }
public static TReturns InvokeCtorGeneric <TReturns>( [NotNull] this IntrospectiveStaticContext <TReturns> @this, [NotNull] MemberDescriptor memberDescriptor, [ItemNotNull] Type[] genericTypeArgs, [ItemCanBeNull] object[] methodArgs) { @this.IsNotNull(nameof(@this)); memberDescriptor.IsNotNull(nameof(memberDescriptor)); var constructorInfo = @this.TargetType.GetConstructor(genericTypeArgs); if (constructorInfo == null) { throw new MissingMemberException(); } var rawReturnValue = constructorInfo.Invoke(methodArgs); if (rawReturnValue is TReturns) { return((TReturns)rawReturnValue); } throw new NotImplementedException(); //var parameterTypes = arguments // .Select(t => t?.GetType()) // .ToArray(); //var constructorInfo = @this.TargetType.GetConstructor( // memberDescriptor, // null, // CallingConventions.Any, // parameterTypes, // new ParameterModifier[] { }); //if (constructorInfo == null) // throw new TypeInitializationException( // @this.TargetType.FullName, null); //var instance = constructorInfo.Invoke(arguments); //var valueType = typeof(TValue); //if (!valueType.IsInstanceOfType(instance)) //{ //} //return (TValue)instance; //throw new InvalidCastException($"Cannot cast property value \'{returnVal.GetType().Name}\' to {typeof(TResult).Name}"); }
public static TReturns InvokeMethodGeneric <TReturns>( [NotNull] this IntrospectiveContext @this, [NotNull] MemberDescriptor memberDescriptor, [NotNull] string methodName, Type[] genericTypeArgs, object[] methodArgs) { @this.IsNotNull(nameof(@this)); memberDescriptor.IsNotNull(nameof(memberDescriptor)); methodName.IsNotNull(nameof(methodName)); var ownerType = @this.TargetType; var methodInfo = ownerType.GetMethod(methodName, memberDescriptor); if (methodInfo == null) { throw new MissingMethodException( ownerType.Name, methodName); } var genericMethodInfo = methodInfo.MakeGenericMethod(genericTypeArgs); if (genericMethodInfo == null) { throw new ArgumentException( $"Unable to create a generic method from the provided " + $"generic type arguments: <" + string.Join(", ", genericTypeArgs .Select( t => t.FormatName()) .ToArray()) + ">.", new MissingMethodException( ownerType.Name, methodName)); } var rawReturnValue = genericMethodInfo.Invoke( @this.TargetObject, methodArgs); if (rawReturnValue is TReturns) { return((TReturns)rawReturnValue); } throw new InvalidCastException( $"Cannot cast raw returned value of type \'{rawReturnValue.GetType().FormatName()}\' " + $"to the expected return type of \'{typeof(TReturns).FormatName()}\'."); }
public static IEnumerable <PropertyInfo> GetProperties( [NotNull] this IntrospectiveStaticContext @this, [NotNull] MemberDescriptor memberDescriptor) { @this.IsNotNull(nameof(@this)); memberDescriptor.IsNotNull(nameof(memberDescriptor)); var propertyInfos = @this .TargetType .GetProperties( memberDescriptor); return(propertyInfos); }
/// <summary> /// Creates an instance of the type with the specified constructor arguments for the Type /// specified in <paramref name="this"/>'s <see cref="IntrospectiveStaticContext.TargetType"/> /// </summary> /// <typeparam name="TValue"> /// The Type of the object being created through the constructor matching the corresponding /// type signature of the arguments provided through the <paramref name="arguments"/> parameter. /// </typeparam> /// <param name="this"> /// The instance of the <see cref="IntrospectiveStaticContext"/> describing the introspection /// context for the <see cref="System.Reflection"/> calls to the constructor. /// </param> /// <param name="descriptor"> /// The <see cref="MemberDescriptor"/> instance specifying the member filtering, fluently /// wrapping the <see cref="BindingFlags"/> configuration to use in the <see cref="System.Reflection"/> /// </param> /// <param name="arguments"> /// An optional param array of <see cref="object"/> that specifies the constructor arguments. /// </param> /// <returns> /// An instance of <typeparamref name="TValue"/> created with the provided constructor arguments. /// </returns> public static TValue CreateInstance<TValue>( [NotNull] this IntrospectiveStaticContext @this, [NotNull] MemberDescriptor descriptor, [ItemCanBeNull] params object[] arguments) { @this.IsNotNull(nameof(@this)); descriptor.IsNotNull(nameof(descriptor)); var instance = CreateInstanceImpl( @this, descriptor, arguments); if (instance is TValue) return (TValue)instance; throw new InvalidCastException( $"Cannot cast property of type \'{instance.GetType().Name}\' to \'{typeof(TValue).Name}\'"); }
public static void InvokeMethod( [NotNull] this IntrospectiveContext @this, [NotNull] MemberDescriptor memberDescriptor, [NotNull] string methodName, params object[] methodArgs) { @this.IsNotNull(nameof(@this)); memberDescriptor.IsNotNull(nameof(memberDescriptor)); methodName.IsNotNull(nameof(methodName)); var ownerType = @this.TargetType; var methodInfo = ownerType.GetMethod(methodName, memberDescriptor); if (methodInfo == null) { throw new MissingMethodException( ownerType.Name, methodName); } methodInfo.Invoke( @this.TargetObject, methodArgs); }
public static void SetFieldValue <TValue>( [NotNull] this IntrospectiveContext @this, [NotNull] MemberDescriptor memberDescriptor, [NotNull] string fieldName, [CanBeNull] TValue value) { @this.IsNotNull(nameof(@this)); memberDescriptor.IsNotNull(nameof(memberDescriptor)); fieldName.IsNotNull(nameof(fieldName)); var ownerType = @this.GetType(); var fieldInfo = ownerType.GetField(fieldName, memberDescriptor); if (fieldInfo == null) { throw new MissingMemberException(ownerType.Name, nameof(fieldName)); } var fieldType = fieldInfo.FieldType; var valueType = typeof(TValue); if (value != null && !valueType.IsValueType) { var defaultValue = valueType.GetDefaultValue(); } if (value == null || value.GetType().IsValueType) { fieldInfo.SetValue(@this, null); } fieldInfo.SetValue(@this, value); //if (!fieldType.IsAssignableFrom(valueType)) //var adjustedValue = value; //throw new InvalidCastException($"Cannot cast property value \'{returnVal.GetType().Name}\' to {typeof(TResult).Name}"); }