// By BJurado, from http://forums.asp.net/t/1709783.aspx/1 internal static MemberExpression StaticPropertyOrField(IReflect type, string propertyOrFieldName) { var property = type.GetProperty(propertyOrFieldName, PropertyStaticPublic); if (property != null) { return(Property(null, property)); } var field = type.GetField(propertyOrFieldName, PropertyStaticPublic); if (field != null) { return(Field(null, field)); } property = type.GetProperty(propertyOrFieldName, PropertyStaticNonPublic); if (property != null) { return(Property(null, property)); } field = type.GetField(propertyOrFieldName, PropertyStaticNonPublic); if (field != null) { return(Field(null, field)); } throw new ArgumentException($"{propertyOrFieldName} NotAMemberOfType {type}"); }
private static MemberInfo[] GetAndWrapMember(IReflect reflect, Object namedItem, String name, BindingFlags bindingAttr) { PropertyInfo property = reflect.GetProperty(name, bindingAttr); if (property != null) { MethodInfo getMethod = JSProperty.GetGetMethod(property, false); MethodInfo setMethod = JSProperty.GetSetMethod(property, false); if ((getMethod != null && !getMethod.IsStatic) || (setMethod != null && !setMethod.IsStatic)) { MethodInfo method = reflect.GetMethod(name, bindingAttr); if (method != null && !method.IsStatic) { MemberInfo[] propMethods = new MemberInfo[1]; propMethods[0] = new JSWrappedPropertyAndMethod(property, method, namedItem); return(propMethods); } } } MemberInfo[] members = reflect.GetMember(name, bindingAttr); if (members != null && members.Length > 0) { return(ScriptObject.WrapMembers(members, namedItem)); } return(null); }
private static PropertyInfo GetPropertyCaseInsensitive(IReflect type, string propertyName) { // make the property reflection lookup case insensitive const BindingFlags bindingFlags = BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance; return type.GetProperty(propertyName, bindingFlags); }
private static bool TryGetProperty(IReflect target, string name, bool ignoreCase, object[] args, out object result) { // ReSharper disable once SuspiciousTypeConversion.Global if (target is IDispatchEx dispatchEx) { // Standard IExpando-over-IDispatchEx support appears to leak the variants it // creates for the invocation arguments. This issue has been reported. In the // meantime we'll bypass this facility and interface with IDispatchEx directly. var value = dispatchEx.GetProperty(name, ignoreCase, args); result = (value is Nonexistent) ? Undefined.Value : value; return(true); } var flags = BindingFlags.Public; if (ignoreCase) { flags |= BindingFlags.IgnoreCase; } var property = target.GetProperty(name, flags); if (property != null) { result = property.GetValue(target, args); return(true); } result = null; return(false); }
private static PropertyInfo GetPropertyCaseInsensitive(IReflect type, string propertyName) { // make the property reflection lookup case insensitive const BindingFlags bindingFlags = BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance; return(type.GetProperty(propertyName, bindingFlags)); }
private static bool TrySetProperty(IReflect target, string name, bool ignoreCase, object[] args, out object result) { if ((args != null) && (args.Length > 0)) { var flags = BindingFlags.Public; if (ignoreCase) { flags |= BindingFlags.IgnoreCase; } var property = target.GetProperty(name, flags); if (property == null) { var expando = target as IExpando; if (expando != null) { property = expando.AddProperty(name); } } if (property != null) { property.SetValue(target, args[args.Length - 1], args.Take(args.Length - 1).ToArray()); result = args[args.Length - 1]; return(true); } } result = null; return(false); }
public static string LookupResource(IReflect resourceManagerProvider, string resourceKey) { PropertyInfo property = resourceManagerProvider.GetProperty(resourceKey, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static); // Fallback with the key name if (property == null) return resourceKey; return (string) property.GetValue(null, null); // returns string directly from res file }
private string GetArray(IReflect reflect) { PropertyInfo piLength = reflect.GetProperty("length", BindingFlags.Default); int len = (int)piLength.GetValue(reflect, null); string[] ret = new string[len]; for (int i = 0; i < len; i++) { PropertyInfo pi = reflect.GetProperty(i.ToString(), BindingFlags.Default); object item; if (pi == null) { item = null; } else { item = pi.GetValue(reflect, null); } ret[i] = GetString(item); } return string.Format("[ {0} ]", string.Join(", ", ret)); }
private static PropertyInfo DeclaringProperty(PropertyInfo property, IReflect declaringType) { var method = property.GetMethod ?? property.SetMethod; var flags = Default; flags |= method.IsStatic ? Static : Instance; flags |= method.IsPublic ? Public : NonPublic; var parameters = property.GetIndexParameters().Select(_ => _.ParameterType).ToArray(); return(declaringType.GetProperty(property.Name, flags, null, property.PropertyType, parameters, null) ?? property); }
/// <summary> /// Creates the format information. /// </summary> /// <param name="format">The format.</param> /// <param name="type">The type.</param> /// <returns>FormatInfo.</returns> private static FormatInfo CreateFormatInfo(string format, IReflect type) { var spilt = format.Split(new[] { ':' }, 2); var param = spilt[0]; var hasSubFormat = spilt.Length == 2; var subFormat = hasSubFormat ? spilt[1] : string.Empty; var propertyInfo = type.GetProperty(param, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase); var outputFormat = propertyInfo != null ? (hasSubFormat ? "{0:" + subFormat + "}" : "{0}") : "{0:" + format + "}"; return(new FormatInfo(propertyInfo, outputFormat)); }
private static MemberInfo GetMember(IReflect entityType, string[] memberNames) { var publicProperties = from name in memberNames select entityType.GetProperty(name, BindingFlags.Instance | BindingFlags.Public) into propertyInfo where propertyInfo != null && IsPropertyOk(propertyInfo) select propertyInfo; var privateProperties = from name in memberNames select entityType.GetProperty(name, BindingFlags.Instance | BindingFlags.NonPublic) into propertyInfo where propertyInfo != null && IsPropertyOk(propertyInfo) select propertyInfo; var publicFields = from name in memberNames select entityType.GetField(name, BindingFlags.Instance | BindingFlags.Public) into fieldInfo where fieldInfo != null && IsFildOk(fieldInfo) select fieldInfo; return(publicProperties.OfType <MemberInfo>().Concat(privateProperties).Concat(publicFields).FirstOrDefault()); }
private static MemberInfo GetMember(IReflect entityType, string[] memberNames) { var publicProperties = from name in memberNames select entityType.GetProperty(name, BindingFlags.Instance | BindingFlags.Public) into propertyInfo where propertyInfo != null && IsPropertyOk(propertyInfo) select propertyInfo; var privateProperties = from name in memberNames select entityType.GetProperty(name, BindingFlags.Instance | BindingFlags.NonPublic) into propertyInfo where propertyInfo != null && IsPropertyOk(propertyInfo) select propertyInfo; var publicFields = from name in memberNames select entityType.GetField(name, BindingFlags.Instance | BindingFlags.Public) into fieldInfo where fieldInfo != null && IsFildOk(fieldInfo) select fieldInfo; return publicProperties.OfType<MemberInfo>().Concat(privateProperties).Concat(publicFields).FirstOrDefault(); }
private static void SetProperty(IReflect type, object instance, string propertyName, object value, BindingFlags flags) { var property = type.GetProperty(propertyName, flags); if (property == null && instance != null) { property = instance.GetType().GetProperty(propertyName, flags); } if (property == null) { throw new ArgumentException($"Target type '{type}' does not contain a definition for property '{propertyName}'."); } property.SetValue(instance ?? type, value, null); }
/// <summary> /// Gets the value of the member from an object using the member name /// </summary> /// <param name="type"></param> /// <param name="memberName">Name of the member</param> /// <param name="obj">Object to get the member value from</param> /// <returns></returns> private static object GetMemberValue(this IReflect type, string memberName, object obj) { var memberInfo = type.GetMember(memberName, DEFAULT_BINDING_FLAGS).FirstOrDefault(); if (memberInfo is PropertyInfo) { return(type.GetProperty(memberName, DEFAULT_BINDING_FLAGS).GetValue(obj)); } if (memberInfo is FieldInfo) { return(type.GetField(memberName, DEFAULT_BINDING_FLAGS).GetValue(obj)); } return(null); }
private static bool TryGetProperty(IReflect type, object instance, string propertyName, BindingFlags flags, out object value) { var property = type.GetProperty(propertyName, flags); if (property == null && instance != null) { property = instance.GetType().GetProperty(propertyName, flags); } if (property == null) { value = default; return(false); } value = property.GetValue(instance ?? type, null); return(true); }
private static bool TrySetProperty(IReflect target, string name, bool ignoreCase, object[] args, out object result) { if ((args != null) && (args.Length > 0)) { // ReSharper disable SuspiciousTypeConversion.Global var dispatchEx = target as IDispatchEx; if (dispatchEx != null) { // Standard IExpando-over-IDispatchEx support appears to leak the variants it // creates for the invocation arguments. This issue has been reported. In the // meantime we'll bypass this facility and interface with IDispatchEx directly. dispatchEx.SetProperty(name, ignoreCase, args); result = args[args.Length - 1]; return(true); } // ReSharper restore SuspiciousTypeConversion.Global var flags = BindingFlags.Public; if (ignoreCase) { flags |= BindingFlags.IgnoreCase; } var property = target.GetProperty(name, flags); if (property == null) { var expando = target as IExpando; if (expando != null) { property = expando.AddProperty(name); } } if (property != null) { property.SetValue(target, args[args.Length - 1], args.Take(args.Length - 1).ToArray()); result = args[args.Length - 1]; return(true); } } result = null; return(false); }
private static MemberInfo[] GetAndWrapMember(IReflect reflect, Object namedItem, String name, BindingFlags bindingAttr){ PropertyInfo property = reflect.GetProperty(name, bindingAttr); if (property != null){ MethodInfo getMethod = JSProperty.GetGetMethod(property, false); MethodInfo setMethod = JSProperty.GetSetMethod(property, false); if ((getMethod != null && !getMethod.IsStatic) || (setMethod != null && !setMethod.IsStatic)){ MethodInfo method = reflect.GetMethod(name, bindingAttr); if (method != null && !method.IsStatic){ MemberInfo[] propMethods = new MemberInfo[1]; propMethods[0] = new JSWrappedPropertyAndMethod(property, method, namedItem); return propMethods; } } } MemberInfo[] members = reflect.GetMember(name, bindingAttr); if (members != null && members.Length > 0) return ScriptObject.WrapMembers(members, namedItem); return null; }
private static bool TryGetProperty(IReflect target, string name, bool ignoreCase, object[] args, out object result) { var flags = BindingFlags.Public; if (ignoreCase) { flags |= BindingFlags.IgnoreCase; } var property = target.GetProperty(name, flags); if (property != null) { result = property.GetValue(target, args); return(true); } result = null; return(false); }
private static MemberInfo[] GetAndWrapMember(IReflect reflect, object namedItem, string name, BindingFlags bindingAttr) { PropertyInfo property = reflect.GetProperty(name, bindingAttr); if (property != null) { MethodInfo getMethod = JSProperty.GetGetMethod(property, false); MethodInfo setMethod = JSProperty.GetSetMethod(property, false); if (((getMethod != null) && !getMethod.IsStatic) || ((setMethod != null) && !setMethod.IsStatic)) { MethodInfo method = reflect.GetMethod(name, bindingAttr); if ((method != null) && !method.IsStatic) { return new MemberInfo[] { new JSWrappedPropertyAndMethod(property, method, namedItem) }; } } } MemberInfo[] member = reflect.GetMember(name, bindingAttr); if ((member != null) && (member.Length > 0)) { return ScriptObject.WrapMembers(member, namedItem); } return null; }
public override MethodInfo GetGetMethod(bool nonPublic) { if (this.getter == null) { try { IReflect superType = ((ClassScope)this.setter.obj).GetSuperType(); BindingFlags @public = BindingFlags.Public; if (this.setter.IsStatic) { @public |= BindingFlags.FlattenHierarchy | BindingFlags.Static; } else { @public |= BindingFlags.Instance; } if (nonPublic) { @public |= BindingFlags.NonPublic; } PropertyInfo prop = superType.GetProperty(this.name, @public, null, null, new Type[0], null); if (prop is JSProperty) { return(prop.GetGetMethod(nonPublic)); } return(GetGetMethod(prop, nonPublic)); } catch (AmbiguousMatchException) { } } if (!nonPublic && !this.getter.IsPublic) { return(null); } return(this.getter); }
private static MemberInfo[] GetAndWrapMember(IReflect reflect, object namedItem, string name, BindingFlags bindingAttr) { PropertyInfo property = reflect.GetProperty(name, bindingAttr); if (property != null) { MethodInfo getMethod = JSProperty.GetGetMethod(property, false); MethodInfo setMethod = JSProperty.GetSetMethod(property, false); if (((getMethod != null) && !getMethod.IsStatic) || ((setMethod != null) && !setMethod.IsStatic)) { MethodInfo method = reflect.GetMethod(name, bindingAttr); if ((method != null) && !method.IsStatic) { return(new MemberInfo[] { new JSWrappedPropertyAndMethod(property, method, namedItem) }); } } } MemberInfo[] member = reflect.GetMember(name, bindingAttr); if ((member != null) && (member.Length > 0)) { return(ScriptObject.WrapMembers(member, namedItem)); } return(null); }
private static void EmitMemberMappings <TSource>(IEnumerable <MemberMapping <TSource> > memberMappings, IReflect targetType, ILGenerator ilGenerator) { int memberMappingIndex = 0; foreach (MemberMapping <TSource> memberMapFunc in memberMappings) { PropertyInfo propertyInfo = targetType.GetProperty(memberMapFunc.MemberName, BindingFlags.Public | BindingFlags.Instance); MethodInfo propertySetMethod = propertyInfo.GetSetMethod(false); if (propertySetMethod == null) { continue; } ilGenerator.Emit(OpCodes.Ldarg_3); // Push target instance onto the stack ilGenerator.Emit(OpCodes.Ldarg_0); // Push delegate array onto stack ilGenerator.Emit(OpCodes.Ldc_I4, memberMappingIndex++); ilGenerator.Emit(OpCodes.Ldelem_Ref); // Get the delegate from the array ilGenerator.Emit(OpCodes.Ldarg_2); // Push the source instance onto the stack ilGenerator.Emit(OpCodes.Callvirt, typeof(Func <TSource, object>).GetMethod("Invoke")); // Invoke the delegate, passing (source) ilGenerator.Emit(propertyInfo.PropertyType.IsValueType ? OpCodes.Unbox_Any : OpCodes.Castclass, propertyInfo.PropertyType); // Cast or unbox the delegate result ilGenerator.Emit(OpCodes.Callvirt, propertySetMethod); // Set the property on the current target instance } }
PropertyInfo IReflect.GetProperty(string name, BindingFlags bindingAttr) { return(publicIReflect.GetProperty(name, bindingAttr)); }
private static PropertyInfo GetSetPropertyInfo(IReflect actionType, string propertyName, string actionId) { var propertyInfo = actionType.GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty | BindingFlags.IgnoreCase); if (propertyInfo == null || (propertyInfo.SetMethod.Attributes & MethodAttributes.Public) != MethodAttributes.Public) { throw new InvalidDataException( string.Format( "Add-on '{0}' doesn't implement public setter for property '{1}' but input property is required for action '{2}'.", actionType, propertyName, actionId)); } return propertyInfo; }
private static PropertyInfo GetTransformNamesPropertyInfo(IReflect attributeType) { return(tranformerNamesLazyPropertyInfo ??= attributeType.GetProperty("TransformNames", BindingFlags.Instance | BindingFlags.Public)); }
PropertyInfo?IReflect.GetProperty(string name, BindingFlags bindingAttr) => publicIReflect.GetProperty(name, bindingAttr);
private static object GetAs(IReflect type, object obj, string prop) { var propInfo = type.GetProperty(prop, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); return propInfo.GetValue(obj, null); }
/// <summary> /// Sets the property. /// </summary> /// <param name="type">The type.</param> /// <param name="strMethod">The STR method.</param> /// <param name="objInstance">The obj instance.</param> /// <param name="eFlags">The e flags.</param> /// <param name="setValue">The set value.</param> /// <param name="index">The index.</param> /// <returns></returns> private static object SetProperty(IReflect type, string strMethod, object objInstance, BindingFlags eFlags, object setValue, object[] index) { PropertyInfo property; try { if ((eFlags == InstanceBindingFlags) && (objInstance == null)) { throw new ArgumentException("The reflection non-static object argument was invalid"); } if ((eFlags == StaticBindingFlags) && (objInstance != null)) { throw new ArgumentException("The reflection static object argument was invalid"); } if ((objInstance != null) && (objInstance.GetType() != (Type)type) && (objInstance.GetType().BaseType != (Type)type)) { throw new ArgumentException("The object instance was of type '" + objInstance.GetType() + "' for type '" + type + "'."); } if (string.IsNullOrEmpty(strMethod)) { throw new ArgumentException("The reflection method argument was invalid"); } property = type.GetProperty(strMethod, eFlags); if (property == null) { throw new ArgumentException("There is no property '" + strMethod + "' for type '" + type + "'."); } if (setValue.GetType() != property.PropertyType) { throw new ArgumentException("The value instance was of type '" + setValue.GetType() + "' for field type '" + property.PropertyType + "'."); } property.SetValue(objInstance, setValue, index); var objRet = property.GetValue(objInstance, index); return objRet; } catch (Exception exception) { Console.WriteLine(exception.Message); throw; } }
PropertyInfo IReflect.GetProperty(string name, BindingFlags bindingAttr) { var ret = typeIReflectImplementation.GetProperty(name, bindingAttr); return(ret); }
private bool SetAuthId(IReflect t, dynamic o) { var mustAuth = t.GetProperty("MustAuth", BindingFlags.Public | BindingFlags.Instance); if (mustAuth != null) { if (Sorted.ContainsKey("session")) { _authorizedId = GetUserId(YunWebUtils.FromBase64StringForUrl(Sorted["session"])); if (_authorizedId > 0) { var authId = t.GetProperty("AuthId", BindingFlags.Public | BindingFlags.Instance); if (authId != null) { o.AuthId = _authorizedId; //authId.SetValue(o, Convert.ChangeType(_authorizedId, TypeCode.Int32), null); return true; } } } //return !(bool)mustAuth.GetValue(o, null); return !o.MustAuth; } return true; }
public static bool HasPublicInstanceProperty(this IReflect type, string name) { Contract.Requires(type != null); return(type.GetProperty(name, BindingFlags.Public | BindingFlags.Instance) != null); }
System.Reflection.PropertyInfo IReflect.GetProperty(string name, System.Reflection.BindingFlags bindingAttr) { return(typeIReflectImplementation.GetProperty(name, bindingAttr)); }
private static object GetAs(IReflect type, object obj, string prop) { var propInfo = type.GetProperty(prop, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); return(propInfo.GetValue(obj, null)); }