public IMethod GetSetMethod() { System.Reflection.MethodInfo setter = _property.GetSetMethod(true); if (null != setter) { return(_typeSystemServices.Map(setter)); } return(null); }
public PropertyInfo(TypeInfo typeInfo, System.Reflection.PropertyInfo info) : base(typeInfo, info, null, typeInfo.TypeCache.GetTypeInfo(info.PropertyType)) { this.callGet = new Lazy <Func <object, object> >(() => { ParameterExpression targetParam = Expression.Parameter(typeof(object), null); Expression castTarget = ReflectionUtility.Convert(targetParam, typeInfo); MemberExpression propertyAccess = Expression.MakeMemberAccess(castTarget, info); Expression castProperty = Expression.Convert(propertyAccess, typeof(object)); return(ReflectionUtility.CompileLambda <Func <object, object> >(castProperty, targetParam)); }); this.callSet = new Lazy <Action <object, object> >(() => { System.Reflection.MethodInfo setMethod = info.GetSetMethod(nonPublic: true); if (setMethod == null) { throw JsonException.New(Resources.Convert_NoSetter, info.Name, info.DeclaringType.FullName); } ParameterExpression targetParam = Expression.Parameter(typeof(object), null); ParameterExpression valueParam = Expression.Parameter(typeof(object), null); Expression castTarget = ReflectionUtility.Convert(targetParam, typeInfo); Expression castValue = Expression.Convert(valueParam, info.PropertyType); Expression propertySet = Expression.Call(castTarget, setMethod, castValue); return(ReflectionUtility.CompileLambda <Action <object, object> >(propertySet, targetParam, valueParam)); }); }
public JsPropertyDefinition(string name, System.Reflection.PropertyInfo propInfo) : base(name, JsMemberKind.Property) { this.propInfo = propInfo; #if NET20 var getter = propInfo.GetGetMethod(true); if (getter != null) { this.GetterMethod = new JsPropertyGetDefinition(name, getter); } var setter = propInfo.GetSetMethod(true); if (setter != null) { this.SetterMethod = new JsPropertySetDefinition(name, setter); } #else var getter = propInfo.GetMethod; if (getter != null) { this.GetterMethod = new JsPropertyGetDefinition(name, getter); } var setter = propInfo.SetMethod; if (setter != null) { this.SetterMethod = new JsPropertySetDefinition(name, setter); } #endif }
private static void AssignToPropertyOrField(object propertyValue, object o, string memberName, JavaScriptSerializer serializer) { System.Collections.IDictionary dictionary = o as System.Collections.IDictionary; if (dictionary != null) { propertyValue = ObjectConverter.ConvertObjectToType(propertyValue, null, serializer); dictionary[memberName] = propertyValue; } else { System.Type type = o.GetType(); System.Reflection.PropertyInfo property = type.GetProperty(memberName, System.Reflection.BindingFlags.IgnoreCase | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public); if (property != null) { System.Reflection.MethodInfo setMethod = property.GetSetMethod(); if (setMethod != null) { propertyValue = ObjectConverter.ConvertObjectToType(propertyValue, property.PropertyType, serializer); setMethod.Invoke(o, new object[] { propertyValue }); return; } } System.Reflection.FieldInfo field = type.GetField(memberName, System.Reflection.BindingFlags.IgnoreCase | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public); if (field != null) { propertyValue = ObjectConverter.ConvertObjectToType(propertyValue, field.FieldType, serializer); field.SetValue(o, propertyValue); } } }
private DictionaryInfo(TypeInfo typeInfo, System.Reflection.PropertyInfo info, TypeInfo keyType, TypeInfo valueType) : base(typeInfo, info, keyType, valueType) { this.info = info; this.callTryGetValue = new Lazy <TryGetValueDelegate>(() => { System.Reflection.MethodInfo tryGetMethod = typeInfo.Type.GetMethod("TryGetValue"); if (tryGetMethod == null) { return(null); } ParameterExpression targetParam = Expression.Parameter(typeof(object), null); ParameterExpression keyParam = Expression.Parameter(typeof(object), null); ParameterExpression outValueParam = Expression.Parameter(typeof(object).MakeByRefType(), null); Expression castTarget = ReflectionUtility.Convert(targetParam, typeInfo); Expression castKey = ReflectionUtility.Convert(keyParam, keyType); Expression callTryGet = Expression.Call(castTarget, tryGetMethod, castKey, outValueParam); return(ReflectionUtility.CompileLambda <TryGetValueDelegate>(callTryGet, targetParam, keyParam, outValueParam)); }); this.callGet = new Lazy <Func <object, object, object> >(() => { System.Reflection.MethodInfo getMethod = info.GetGetMethod(nonPublic: false); if (getMethod == null) { return(null); } ParameterExpression targetParam = Expression.Parameter(typeof(object), null); ParameterExpression keyParam = Expression.Parameter(typeof(object), null); Expression castTarget = ReflectionUtility.Convert(targetParam, typeInfo); Expression castKey = ReflectionUtility.Convert(keyParam, keyType); Expression callGet = Expression.Call(castTarget, getMethod, castKey); return(ReflectionUtility.CompileLambda <Func <object, object, object> >(callGet, targetParam, keyParam)); }); this.callSet = new Lazy <Action <object, object, object> >(() => { System.Reflection.MethodInfo setMethod = info.GetSetMethod(nonPublic: false); if (setMethod == null) { return(null); } ParameterExpression targetParam = Expression.Parameter(typeof(object), null); ParameterExpression keyParam = Expression.Parameter(typeof(object), null); ParameterExpression valueParam = Expression.Parameter(typeof(object), null); Expression castTarget = ReflectionUtility.Convert(targetParam, typeInfo); Expression castKey = ReflectionUtility.Convert(keyParam, keyType); Expression castValue = ReflectionUtility.Convert(valueParam, valueType); Expression callSet = Expression.Call(castTarget, setMethod, castKey, castValue); return(ReflectionUtility.CompileLambda <Action <object, object, object> >(callSet, targetParam, keyParam, valueParam)); }); }
public void System_PropertyInfo_is_wrapped_by_Routine_PropertyInfo() { Assert.AreEqual(propertyInfo.Name, testing.Name); Assert.AreEqual(propertyInfo.GetGetMethod()?.Name, testing.GetGetMethod().Name); Assert.AreEqual(propertyInfo.GetSetMethod()?.Name, testing.GetSetMethod().Name); Assert.AreSame(propertyInfo.DeclaringType, testing.DeclaringType.GetActualType()); Assert.AreSame(propertyInfo.ReflectedType, testing.ReflectedType.GetActualType()); Assert.AreSame(propertyInfo.PropertyType, testing.PropertyType.GetActualType()); }
private static System.Xml.XmlNode MakePropertyNode(System.Reflection.PropertyInfo memberInfo, System.Xml.XmlDocument ownerdoc) { System.Xml.XmlNode nodeMember; nodeMember = Utility.xmlHelpers.NewElement(ownerdoc, "Property", memberInfo.Name); nodeMember.Attributes.Append(Utility.xmlHelpers.NewAttribute(ownerdoc, "Type", memberInfo.PropertyType.ToString())); nodeMember.Attributes.Append(Utility.xmlHelpers.NewBoolAttribute(ownerdoc, "CanRead", memberInfo.CanRead)); nodeMember.Attributes.Append(Utility.xmlHelpers.NewBoolAttribute(ownerdoc, "CanWrite", memberInfo.CanWrite)); MakeParameterNodes(nodeMember, memberInfo.GetIndexParameters()); if (memberInfo.GetGetMethod() != null) { nodeMember.AppendChild(MakeMethodNode(memberInfo.GetGetMethod(), ownerdoc, false)); } if (memberInfo.GetSetMethod() != null) { nodeMember.AppendChild(MakeMethodNode(memberInfo.GetSetMethod(), ownerdoc, false)); } return(nodeMember); }
System.Reflection.MethodInfo GetAccessor() { System.Reflection.MethodInfo mi = _property.GetGetMethod(true); if (null != mi) { return(mi); } return(_property.GetSetMethod(true) ); }
/// <summary> /// 对象自拷贝 /// </summary> /// <param name="obj"></param> /// <returns></returns> public static object SelfCopy(this object obj) { Object targetDeepCopyObj; Type targetType = obj.GetType(); //值类型 if (targetType.IsValueType == true) { targetDeepCopyObj = obj; } //引用类型 else { targetDeepCopyObj = System.Activator.CreateInstance(targetType); //创建引用对象 System.Reflection.MemberInfo[] memberCollection = obj.GetType().GetMembers(); foreach (System.Reflection.MemberInfo member in memberCollection) { if (member.MemberType == System.Reflection.MemberTypes.Field) { System.Reflection.FieldInfo field = (System.Reflection.FieldInfo)member; Object fieldValue = field.GetValue(obj); if (fieldValue is ICloneable) { field.SetValue(targetDeepCopyObj, (fieldValue as ICloneable).Clone()); } else { field.SetValue(targetDeepCopyObj, SelfCopy(fieldValue)); } } else if (member.MemberType == System.Reflection.MemberTypes.Property) { System.Reflection.PropertyInfo myProperty = (System.Reflection.PropertyInfo)member; System.Reflection.MethodInfo info = myProperty.GetSetMethod(false); if (info != null) { object propertyValue = myProperty.GetValue(obj, null); if (propertyValue is ICloneable) { myProperty.SetValue(targetDeepCopyObj, (propertyValue as ICloneable).Clone(), null); } else { myProperty.SetValue(targetDeepCopyObj, SelfCopy(propertyValue), null); } } } } } return(targetDeepCopyObj); }
//System.Reflection.PropertyInfo prop; /// <summary> /// ManagedProperty インスタンスを PropertyInfo から作成します。 /// </summary> /// <param name="prop">PropertyInfo</param> public ManagedProperty(System.Reflection.PropertyInfo prop) { //this.prop=prop; System.Type type = prop.ReflectedType; if (prop.CanRead) { this[":propget:"] = new ManagedMethod(type, prop.GetGetMethod()); } if (prop.CanWrite) { this[":propput:"] = new ManagedMethod(type, prop.GetSetMethod()); } }
private static object GetEntity <TData>(TData data, System.Type entityType, Func <TData, string, object> func) where TData : class { object result; if (data == null) { result = null; } else { EntityClassAttribute entityClassAttribute = JavaScriptSerializer.GetEntityClassAttribute(entityType); System.Reflection.PropertyInfo[] properties = entityType.GetProperties(); object obj = System.Activator.CreateInstance(entityType); System.Reflection.PropertyInfo[] array = properties; for (int i = 0; i < array.Length; i++) { System.Reflection.PropertyInfo propertyInfo = array[i]; System.Reflection.MethodInfo setMethod = propertyInfo.GetSetMethod(); if (!(setMethod == null)) { string propertyKey = JavaScriptSerializer.GetPropertyKey(propertyInfo, entityClassAttribute); System.Type propertyType = propertyInfo.PropertyType; object value = EntityFactory.GetValue(func(data, propertyKey), propertyType); if (value != null) { if (propertyType.IsGenericType) { if (EntityFactory.IsIList(propertyType)) { System.Type type = propertyType.GetGenericArguments()[0]; propertyInfo.SetValue(obj, EntityFactory.GetEntities <TData>(value as System.Collections.IEnumerable, type, func), null); } } else if (!EntityFactory.IsPrimitiveType(propertyType)) { propertyInfo.SetValue(obj, EntityFactory.GetEntity <TData>(value as TData, propertyType, func), null); } else { setMethod.Invoke(obj, new object[] { value }); } } } } result = obj; } return(result); }
protected override Newtonsoft.Json.Serialization.JsonProperty CreateProperty(System.Reflection.MemberInfo member, MemberSerialization memberSerialization) { Newtonsoft.Json.Serialization.JsonProperty prop = base.CreateProperty(member, memberSerialization); if (!prop.Writable) { System.Reflection.PropertyInfo property = member as System.Reflection.PropertyInfo; if (property != null) { bool hasPrivateSetter = property.GetSetMethod(true) != null; prop.Writable = hasPrivateSetter; } } return(prop); }
/// <summary> /// Initializes a new OSpace instance of the property class /// </summary> /// <param name="name">name of the property</param> /// <param name="typeUsage">TypeUsage object containing the property type and its facets</param> /// <param name="propertyInfo">for the property</param> /// <param name="entityDeclaringType">The declaring type of the entity containing the property</param> internal EdmProperty(string name, TypeUsage typeUsage, System.Reflection.PropertyInfo propertyInfo, RuntimeTypeHandle entityDeclaringType) : this(name, typeUsage) { System.Diagnostics.Debug.Assert(name == propertyInfo.Name, "different PropertyName"); if (null != propertyInfo) { System.Reflection.MethodInfo method; method = propertyInfo.GetGetMethod(true); // return public or non-public getter PropertyGetterHandle = ((null != method) ? method.MethodHandle : default(System.RuntimeMethodHandle)); method = propertyInfo.GetSetMethod(true); // return public or non-public getter PropertySetterHandle = ((null != method) ? method.MethodHandle : default(System.RuntimeMethodHandle)); EntityDeclaringType = entityDeclaringType; } }
// Ctor for imported properties public PropertyExpEntry( ISemanticResolver s, System.Reflection.PropertyInfo info ) { m_info = info; m_strName = m_info.Name; // Class that we're defined in? System.Type tClrClass = info.DeclaringType; m_tClassDefined = s.ResolveCLRTypeToBlueType(tClrClass); // Symbol type this.m_type = s.ResolveCLRTypeToBlueType(info.PropertyType); // Spoof accessors if (info.CanRead) // Has Get { System.Reflection.MethodInfo mGet = info.GetGetMethod(); m_symbolGet = new MethodExpEntry(s, mGet); } if (info.CanWrite) // Has Set { System.Reflection.MethodInfo mSet = info.GetSetMethod(); m_symbolSet = new MethodExpEntry(s, mSet); } // Get modifiers System.Reflection.MethodInfo [] m = info.GetAccessors(); m_mods = new Modifiers(m[0]); /* * m_mods = new Modifiers(); * if (m[0].IsStatic) m_mods.SetStatic(); * if (m[0].IsAbstract) m_mods.SetAbstract(); * if (m[0].IsVirtual) m_mods.SetVirtual(); */ }
public override bool Process(PropertyInfo property, IMethodRemover methodRemover, IFacetHolder holder) { string capitalizedName = property.Name; var paramTypes = new[] {property.PropertyType}; var facets = new List<IFacet> {new PropertyAccessorFacetViaAccessor(property, holder)}; if (property.PropertyType.IsGenericType && (property.PropertyType.GetGenericTypeDefinition() == typeof (Nullable<>))) { facets.Add(new NullableFacetAlways(holder)); } if (property.GetSetMethod() != null) { if (property.PropertyType == typeof (byte[])) { facets.Add(new DisabledFacetAlways(holder)); } else { facets.Add(new PropertySetterFacetViaSetterMethod(property, holder)); } facets.Add(new PropertyInitializationFacetViaSetterMethod(property, holder)); } else { //facets.Add(new DerivedFacetInferred(holder)); facets.Add(new NotPersistedFacetAnnotation(holder)); facets.Add(new DisabledFacetAlways(holder)); } FindAndRemoveModifyMethod(facets, methodRemover, property.DeclaringType, capitalizedName, paramTypes, holder); FindAndRemoveClearMethod(facets, methodRemover, property.DeclaringType, capitalizedName, holder); FindAndRemoveAutoCompleteMethod(facets, methodRemover, property.DeclaringType, capitalizedName, property.PropertyType, holder); FindAndRemoveChoicesMethod(facets, methodRemover, property.DeclaringType, capitalizedName, property.PropertyType, holder); FindAndRemoveDefaultMethod(facets, methodRemover, property.DeclaringType, capitalizedName, property.PropertyType, holder); FindAndRemoveValidateMethod(facets, methodRemover, property.DeclaringType, paramTypes, capitalizedName, holder); AddHideForSessionFacetNone(facets, holder); AddDisableForSessionFacetNone(facets, holder); FindDefaultHideMethod(facets, methodRemover, property.DeclaringType, MethodType.Object, "PropertyDefault", new Type[0], holder); FindAndRemoveHideMethod(facets, methodRemover, property.DeclaringType, MethodType.Object, capitalizedName, property.PropertyType, holder); FindDefaultDisableMethod(facets, methodRemover, property.DeclaringType, MethodType.Object, "PropertyDefault", new Type[0], holder); FindAndRemoveDisableMethod(facets, methodRemover, property.DeclaringType, MethodType.Object, capitalizedName, property.PropertyType, holder); return FacetUtils.AddFacets(facets); }
public static bool SetPropertyValue(this object instance, string PropertyName, object PropertyValue) { if (PropertyName == null) { return(false); } Type type = instance.GetType(); System.Reflection.PropertyInfo property = type.GetProperty(PropertyName); if (property == null) { return(false); } System.Reflection.MethodInfo method = property.GetSetMethod(); if (method == null) { return(false); } method.Invoke(instance, new object[] { PropertyValue }); return(true); }
internal CILPropertyImpl( CILReflectionContextImpl ctx, Int32 anID, System.Reflection.PropertyInfo pInfo) : base(ctx, anID, CILElementKind.Property, () => new CustomAttributeDataEventArgs(ctx, pInfo)) { ArgumentValidator.ValidateNotNull("Property", pInfo); if (pInfo.DeclaringType #if WINDOWS_PHONE_APP .GetTypeInfo() #endif .IsGenericType&& !pInfo.DeclaringType #if WINDOWS_PHONE_APP .GetTypeInfo() #endif .IsGenericTypeDefinition) { throw new ArgumentException("This constructor may be used only on properties declared in genericless types or generic type definitions."); } InitFields( ref this.name, ref this.propertyAttributes, ref this.setMethod, ref this.getMethod, ref this.declaringType, ref this.constValue, ref this.customModifiers, new SettableValueForClasses <String>(pInfo.Name), new SettableValueForEnums <PropertyAttributes>((PropertyAttributes)pInfo.Attributes), () => ctx.Cache.GetOrAdd(pInfo.GetSetMethod(true)), () => ctx.Cache.GetOrAdd(pInfo.GetGetMethod(true)), () => (CILType)ctx.Cache.GetOrAdd(pInfo.DeclaringType), new SettableLazy <Object>(() => ctx.LaunchConstantValueLoadEvent(new ConstantValueLoadArgs(pInfo))), ctx.LaunchEventAndCreateCustomModifiers(new CustomModifierEventLoadArgs(pInfo)), true ); }
//THIS DOESNT WORK - REMOVED FROM THE CHECK FUNCTION FOR NOW bool ProcessStructVariableTypes(object input, int inputType) { if (inputType == 0) { System.Reflection.PropertyInfo p = input as System.Reflection.PropertyInfo; Debug.Log("name: " + p.Name + " attributes: " + p.Attributes + " declaring type: " + p.DeclaringType + " accessors: " + p.GetAccessors() + " attributes: " + p.GetCustomAttributes(true) + " getmethod: " + p.GetGetMethod() + " setmethod: " + p.GetSetMethod() + " type: " + p.GetType() + " membertype: " + p.MemberType + " module: " + p.Module + " proptype: " + p.PropertyType + " reftype: " + p.ReflectedType); CompleteVariableSetup(16); return(false); } return(false); }
private Action <object, object> ISet() { var instance = System.Linq.Expressions.Expression.Parameter(typeof(object), "instance"); var value = System.Linq.Expressions.Expression.Parameter(typeof(object), "value"); System.Linq.Expressions.UnaryExpression instanceCast = (!Data.DeclaringType.IsValueType) ? System.Linq.Expressions.Expression.TypeAs(instance, Data.DeclaringType) : System.Linq.Expressions.Expression.Convert(instance, Data.DeclaringType); System.Linq.Expressions.UnaryExpression valueCast = (!Data.PropertyType.IsValueType) ? System.Linq.Expressions.Expression.TypeAs(value, Data.PropertyType) : System.Linq.Expressions.Expression.Convert(value, Data.PropertyType); return(System.Linq.Expressions.Expression.Lambda <Action <object, object> >(System.Linq.Expressions.Expression.Call(instanceCast, Data.GetSetMethod(), valueCast), new System.Linq.Expressions.ParameterExpression[] { instance, value }).Compile()); }
public RMCommandParser(string commandLine, object classForAutoAttributes) { m_commandLine = commandLine; Type type = classForAutoAttributes.GetType(); System.Reflection.MemberInfo[] members = type.GetMembers(); for (int i = 0; i < members.Length; i++) { object[] attributes = members[i].GetCustomAttributes(false); if (attributes.Length > 0) { SwitchRecord rec = null; foreach (Attribute attribute in attributes) { if (attribute is CommandLineSwitchAttribute) { CommandLineSwitchAttribute switchAttrib = (CommandLineSwitchAttribute)attribute; // Get the property information. We're only handling // properties at the moment! if (members[i] is System.Reflection.PropertyInfo) { System.Reflection.PropertyInfo pi = (System.Reflection.PropertyInfo)members[i]; rec = new SwitchRecord(switchAttrib.Name, switchAttrib.Description, pi.PropertyType); // Map in the Get/Set methods. rec.SetMethod = pi.GetSetMethod(); rec.GetMethod = pi.GetGetMethod(); rec.PropertyOwner = classForAutoAttributes; // Can only handle a single switch for each property // (otherwise the parsing of aliases gets silly...) break; } } } // See if any aliases are required. We can only do this after // a switch has been registered and the framework doesn't make // any guarantees about the order of attributes, so we have to // walk the collection a second time. if (rec != null) { foreach (Attribute attribute in attributes) { if (attribute is CommandLineAliasAttribute) { CommandLineAliasAttribute aliasAttrib = (CommandLineAliasAttribute)attribute; rec.AddAlias(aliasAttrib.Alias); } } } // Assuming we have a switch record (that may or may not have // aliases), add it to the collection of switches. if (rec != null) { if (m_switches == null) { m_switches = new System.Collections.ArrayList(); } m_switches.Add(rec); } } } }
public void _Mod_SetPropertyValue(String name, Object value) { System.Reflection.PropertyInfo prop = this.GetType().GetProperty(name); prop.SetValue(prop.GetSetMethod().IsStatic ? null : this, value, null); }
public void InvokeSet(object target) { _property.GetSetMethod().Invoke(target, new object[] { ValueAsInt32 }); }
public void InvokeSet(object target, object value) { _property.GetSetMethod().Invoke(target, new object[] { value }); }