System.DefaultBinder.BindToField C# (CSharp) Method

BindToField() public method

public BindToField ( BindingFlags bindingAttr, FieldInfo match, Object value, System.Globalization.CultureInfo cultureInfo ) : FieldInfo
bindingAttr BindingFlags
match System.Reflection.FieldInfo
value Object
cultureInfo System.Globalization.CultureInfo
return System.Reflection.FieldInfo
        public override FieldInfo BindToField(BindingFlags bindingAttr,FieldInfo[] match, Object value,CultureInfo cultureInfo)
        {
            int i;
            // Find the method that match...
            int CurIdx = 0;

            Type valueType = null;
            
            // If we are a FieldSet, then use the value's type to disambiguate
            if ((bindingAttr & BindingFlags.SetField) != 0) {
                valueType = value.GetType();
                
                for (i=0;i<match.Length;i++) {
                    Type pCls = match[i].FieldType;
                    if (pCls == valueType) {
                        match[CurIdx++] = match[i];
                        continue;
                    }
                    if (value == Empty.Value) {
                        // the object passed in was null which would match any non primitive non value type
                        if (pCls.IsClass) {
                            match[CurIdx++] = match[i];
                            continue;
                        }
                    }
                    if (pCls == typeof(Object)) {
                        match[CurIdx++] = match[i];
                        continue;
                    }
                    if (pCls.IsPrimitive) {
                        if (CanConvertPrimitiveObjectToType(value,(RuntimeType)pCls)) {
                            match[CurIdx++] = match[i];
                            continue;
                        }
                    }
                    else {
                        if (pCls.IsAssignableFrom(valueType)) {
                            match[CurIdx++] = match[i];
                            continue;
                        }
                    }
                }
                if (CurIdx == 0)
                    throw new MissingFieldException(Environment.GetResourceString("MissingField"));
                if (CurIdx == 1)
                    return match[0];
            }
            
            // Walk all of the methods looking the most specific method to invoke
            int currentMin = 0;
            bool ambig = false;
            for (i=1;i<CurIdx;i++) {
                int newMin = FindMostSpecificField(match[currentMin], match[i]);
                if (newMin == 0)
                    ambig = true;
                else {
                    if (newMin == 2) {
                        currentMin = i;
                        ambig = false;
                    }
                }
            }
            if (ambig)
                throw new AmbiguousMatchException(Environment.GetResourceString("RFLCT.Ambiguous"));
            return match[currentMin];
        }