mardi 17 août 2021

C#, FieldSetter, FieldGetter, GetFieldInfo methods in Object class

What is the purpose of these methods: Object.FieldSetter, Object.FieldGetter, Object.GetFieldInfo? Why are they located in the Object class and not in some reflection .net api? Couldn't find it in google or stackoverflow. Seems like nobody ever got interested in them. Also couldn't find usages of these methods. So they seem to be useless, but might be invoked using reflection. I'm just interested if anyone knows.

    private void FieldSetter(string typeName, string fieldName, object val)
    {
      FieldInfo fieldInfo = this.GetFieldInfo(typeName, fieldName);
      if (fieldInfo.IsInitOnly)
        throw new FieldAccessException(Environment.GetResourceString("FieldAccess_InitOnly"));
      Message.CoerceArg(val, fieldInfo.FieldType);
      fieldInfo.SetValue(this, val);
    }

    private void FieldGetter(string typeName, string fieldName, ref object val)
    {
      FieldInfo fieldInfo = this.GetFieldInfo(typeName, fieldName);
      val = fieldInfo.GetValue(this);
    }

    private FieldInfo GetFieldInfo(string typeName, string fieldName)
    {
      Type type = this.GetType();
      while ((Type) null != type && !type.FullName.Equals(typeName))
        type = type.BaseType;
      FieldInfo fieldInfo = !((Type) null == type) ? type.GetField(fieldName, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public) : throw new RemotingException(string.Format((IFormatProvider) CultureInfo.CurrentCulture, Environment.GetResourceString("Remoting_BadType"), (object) typeName));
      return !((FieldInfo) null == fieldInfo) ? fieldInfo : throw new RemotingException(string.Format((IFormatProvider) CultureInfo.CurrentCulture, Environment.GetResourceString("Remoting_BadField"), (object) fieldName, (object) typeName));
    }




Aucun commentaire:

Enregistrer un commentaire