FRBDK/Glue/FlatRedBall.PropertyGrid/LateBinder.cs
/// <summary>
/// Sets the supplied property on the supplied target
/// </summary>
/// <typeparam name="K">the type of the value</typeparam>
Sets the supplied property on the supplied target
Sets the supplied property on the supplied target
public void SetProperty<K>(object target, string propertyName, K value)
        {
#if NO_CODE_EMIT
            Type type = typeof(T);
            PropertyInfo propertyInfo = type.GetProperty(propertyName);
            if (propertyInfo != null)
            {
                propertyInfo.SetValue(target, value, null);
            }
            else
            {
                FieldInfo fieldInfo = type.GetField(propertyName);
                if (fieldInfo != null)
                {
                    fieldInfo.SetValue(target, value);
                }
                else
                {
                    throw new ArgumentException("Cannot find property or field with the name " + propertyName);
                }
            }
#else
            ValidateSetter(ref propertyName);
            if (mPropertySet.ContainsKey(propertyName))
            {
                mPropertySet[propertyName](target, value);
            }
            else
            {
                FieldInfo fieldInfo = mType.GetField(propertyName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance);
                if (fieldInfo == null)
                {
                    string errorMessage =
                        "LateBinder could not find a field or property by the name of " + propertyName +
                        ".  Check the name of the property to verify if it is correct.";
                    throw new System.MemberAccessException(errorMessage);
                }
                else
                {
#if WINDOWS_8
                    fieldInfo.SetValue(target, value);
#else
                    if (!fieldInfo.IsPublic)
                    {
                        fieldInfo.SetValue(target, value);
                    }
                    else
                    {
                        object[] args = { value };
                        mType.InvokeMember(propertyName, BindingFlags.SetField, null, target, args);
                    }
#endif
                }
            }
#endif
        }
public void SetProperty < K > ( object target , string propertyName , K value ) { if NO_CODE_EMIT Type type = typeof ( T ) ; PropertyInfo propertyInfo = type . GetProperty ( propertyName ) ; if ( propertyInfo != null ) { propertyInfo . SetValue ( target , value , null ) ; } else { FieldInfo fieldInfo = type . GetField ( propertyName ) ; if ( fieldInfo != null ) { fieldInfo . SetValue ( target , value ) ; } else { throw new ArgumentException ( " " + propertyName ) ; } } else ValidateSetter ( ref propertyName ) ; if ( mPropertySet . ContainsKey ( propertyName ) ) { mPropertySet [ propertyName ] ( target , value ) ; } else { FieldInfo fieldInfo = mType . GetField ( propertyName , BindingFlags . Public | BindingFlags . NonPublic | BindingFlags . Static | BindingFlags . Instance ) ; if ( fieldInfo == null ) { string errorMessage = " " + propertyName + " " ; throw new System . MemberAccessException ( errorMessage ) ; } else { if WINDOWS_8 fieldInfo . SetValue ( target , value ) ; else if ( ! fieldInfo . IsPublic ) { fieldInfo . SetValue ( target , value ) ; } else { object [ ] args = { value } ; mType . InvokeMember ( propertyName , BindingFlags . SetField , null , target , args ) ; } endif } } endif }
Sets the supplied property on the supplied target
Sets the supplied property on the supplied target
// find out if this is a property or field // This is probably not a property so see if it is a field. // I don't know why we branch here....Can we not call SetValue on public values?
target
object
propertyName
string
value
K
typeparam
the type of the value
the type of the value