usage property net custom attribute asp c# .net reflection .net-attributes

property - reflection c#



Encuentre un campo privado con Reflection? (10)

Dada esta clase

class Foo { // Want to find _bar with reflection [SomeAttribute] private string _bar; public string BigBar { get { return this._bar; } } }

Quiero encontrar la barra privada de elementos que marcaré con un atributo. ¿Es eso posible?

He hecho esto con propiedades donde he buscado un atributo, pero nunca un campo de miembro privado.

¿Cuáles son las banderas de encuadernación que necesito configurar para obtener los campos privados?


Buena sintaxis con el método de extensión

Puede acceder a cualquier campo privado de un tipo arbitrario con un código como este:

Foo foo = new Foo(); string c = foo.GetFieldValue<string>("_bar");

Para eso, necesita definir un método de extensión que haga el trabajo por usted:

public static class RefrelctionExtensions { public static T GetFieldValue<T>(this object obj, string name) { // Set the flags so that private and public fields from instances will be found var bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance; var field = obj.GetType().GetField(name, bindingFlags); return (T)field?.GetValue(obj); } }


Aquí hay algunos métodos de extensión para obtener y configurar campos privados y propiedades (propiedades con setter):

ejemplo de uso:

public class Foo { private int Bar = 5; } var targetObject = new Foo(); var barValue = targetObject.GetMemberValue("Bar");//Result is 5 targetObject.SetMemberValue("Bar", 10);//Sets Bar to 10

Código:

/// <summary> /// Extensions methos for using reflection to get / set member values /// </summary> public static class ReflectionExtensions { /// <summary> /// Gets the public or private member using reflection. /// </summary> /// <param name="obj">The source target.</param> /// <param name="memberName">Name of the field or property.</param> /// <returns>the value of member</returns> public static object GetMemberValue(this object obj, string memberName) { var memInf = GetMemberInfo(obj, memberName); if (memInf == null) throw new System.Exception("memberName"); if (memInf is System.Reflection.PropertyInfo) return memInf.As<System.Reflection.PropertyInfo>().GetValue(obj, null); if (memInf is System.Reflection.FieldInfo) return memInf.As<System.Reflection.FieldInfo>().GetValue(obj); throw new System.Exception(); } /// <summary> /// Gets the public or private member using reflection. /// </summary> /// <param name="obj">The target object.</param> /// <param name="memberName">Name of the field or property.</param> /// <returns>Old Value</returns> public static object SetMemberValue(this object obj, string memberName, object newValue) { var memInf = GetMemberInfo(obj, memberName); if (memInf == null) throw new System.Exception("memberName"); var oldValue = obj.GetMemberValue(memberName); if (memInf is System.Reflection.PropertyInfo) memInf.As<System.Reflection.PropertyInfo>().SetValue(obj, newValue, null); else if (memInf is System.Reflection.FieldInfo) memInf.As<System.Reflection.FieldInfo>().SetValue(obj, newValue); else throw new System.Exception(); return oldValue; } /// <summary> /// Gets the member info /// </summary> /// <param name="obj">source object</param> /// <param name="memberName">name of member</param> /// <returns>instanse of MemberInfo corresponsing to member</returns> private static System.Reflection.MemberInfo GetMemberInfo(object obj, string memberName) { var prps = new System.Collections.Generic.List<System.Reflection.PropertyInfo>(); prps.Add(obj.GetType().GetProperty(memberName, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.FlattenHierarchy)); prps = System.Linq.Enumerable.ToList(System.Linq.Enumerable.Where( prps,i => !ReferenceEquals(i, null))); if (prps.Count != 0) return prps[0]; var flds = new System.Collections.Generic.List<System.Reflection.FieldInfo>(); flds.Add(obj.GetType().GetField(memberName, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.FlattenHierarchy)); //to add more types of properties flds = System.Linq.Enumerable.ToList(System.Linq.Enumerable.Where(flds, i => !ReferenceEquals(i, null))); if (flds.Count != 0) return flds[0]; return null; } [System.Diagnostics.DebuggerHidden] private static T As<T>(this object obj) { return (T)obj; } }


Me encontré con esto mientras buscaba esto en google, así que me doy cuenta de que estoy superando una publicación anterior. Sin embargo, GetCustomAttributes requiere dos params.

typeof(Foo).GetFields(BindingFlags.NonPublic | BindingFlags.Instance) .Where(x => x.GetCustomAttributes(typeof(SomeAttribute), false).Length > 0);

El segundo parámetro especifica si desea o no buscar en la jerarquía de herencia


Obtenga el valor de la variable privada con Reflection:

var _barVariable = typeof(Foo).GetField("_bar", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(objectForFooClass);

Establecer valor para variable privada usando Reflection:

typeof(Foo).GetField("_bar", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(objectForFoocClass, "newValue");

Donde objectForFooClass es una instancia no nula para el tipo de clase Foo.


Puedes hacerlo igual que con una propiedad:

FieldInfo fi = typeof(Foo).GetField("_bar", BindingFlags.NonPublic | BindingFlags.Instance); if (fi.GetCustomAttributes(typeof(SomeAttribute)) != null) ...


Sí, sin embargo, tendrá que establecer sus banderas de Enlazado para buscar campos privados (si busca el miembro fuera de la instancia de la clase).

La bandera de enlace que necesitará es: System.Reflection.BindingFlags.NonPublic


Una cosa que debe tener en cuenta cuando reflexiona sobre miembros privados es que si su aplicación se ejecuta en confianza media (como, por ejemplo, cuando se ejecuta en un entorno de alojamiento compartido), no los encontrará: el La opción BindingFlags.NonPublic simplemente será ignorada.


Utilice BindingFlags.NonPublic y BindingFlags.Instance flags

FieldInfo[] fields = myType.GetFields( BindingFlags.NonPublic | BindingFlags.Instance);


Yo uso este método personalmente

if (typeof(Foo).GetFields(BindingFlags.NonPublic | BindingFlags.Instance).Any(c => c.GetCustomAttributes(typeof(SomeAttribute), false).Any())) { // do stuff }


typeof(MyType).GetField("fieldName", BindingFlags.NonPublic | BindingFlags.Instance)