문제

이 수업을 들으면서

class Foo
{
    // Want to find _bar with reflection
    [SomeAttribute]
    private string _bar;

    public string BigBar
    {
        get { return this._bar; }
    }
}

속성으로 표시할 개인 항목 _bar를 찾고 싶습니다.그게 가능합니까?

속성을 검색했지만 개인 멤버 필드는 검색하지 않은 속성을 사용하여 이 작업을 수행했습니다.

비공개 필드를 가져오기 위해 설정해야 하는 바인딩 플래그는 무엇입니까?

도움이 되었습니까?

해결책

사용 BindingFlags.NonPublic 그리고 BindingFlags.Instance 깃발

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

다른 팁

속성과 마찬가지로 이를 수행할 수 있습니다.

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

Reflection을 사용하여 개인 변수의 값을 가져옵니다.

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

Reflection을 사용하여 개인 변수의 값을 설정합니다.

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

여기서 objectForFooClass는 클래스 유형 Foo에 대한 null이 아닌 인스턴스입니다.

private 멤버를 고려할 때 알아야 할 한 가지는 응용 프로그램이 중간 신뢰에서 실행되는 경우(예를 들어 공유 호스팅 환경에서 실행하는 경우) 해당 멤버를 찾을 수 없다는 것입니다. BindingFlags.NonPublic 옵션은 무시됩니다.

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

확장 방법을 사용한 좋은 구문

다음과 같은 코드를 사용하여 임의 유형의 비공개 필드에 액세스할 수 있습니다.

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

이를 위해서는 작업을 수행할 확장 메서드를 정의해야 합니다.

public static class ReflectionExtensions {
    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);
    }
}

저는 개인적으로 이 방법을 사용하고 있어요

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

예, 그러나 비공개 필드를 검색하려면 바인딩 플래그를 설정해야 합니다(클래스 인스턴스 외부에서 멤버를 찾는 경우).

필요한 바인딩 플래그는 다음과 같습니다.시스템.반사.BindingFlags.NonPublic

다음은 간단한 비공개 필드 및 속성(setter가 있는 속성)을 가져오고 설정하기 위한 몇 가지 확장 메서드입니다.

사용 예:

    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

암호:

    /// <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;
        }
    }

구글에서 검색하다가 이 글을 보게 되었는데, 오래된 글을 발견하게 되었습니다.그러나 GetCustomAttributes에는 두 개의 매개변수가 필요합니다.

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

두 번째 매개변수는 상속 계층 구조를 검색할지 여부를 지정합니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top