jeudi 20 décembre 2018

Getting fields by reflection asynchronously

I am trying to get public fields using reflection from asembly.

Assembly contain just one class as below:

  public abstract class User
    {
        private object _iAmPrivateField;
        protected object IAmProtectedField;
        internal object IAmInternalField;
        public object IAmPublicField;

        private void IAmPrivateMethod() { }
        protected void IAmProtectedMethod() { }
        internal void IAmInternalMethod() { }
        public void IAmPublicMethod() { }

        private object IAmPrivateProperty { get; set; }
        protected object IAmProtectedProperty { get; set; }
        internal object IAmInternalProperty { get; set; }
        public object IAmPublicProperty { get; set; }              
    }

This is the method which retrives public fields from given assembly:

public FieldInfo[] GetPublic(Assembly assembly)
{
     return assembly.GetTypes()
                    .SelectMany(x => x.GetFields(BindingFlags.Public | BindingFlags.Instance))
                    .Where(x => x.IsPublic).ToArray();
}

The above example is working as expected - the result is 1.

However I added async method inside the class

public async Task IAmAsyncMethod() { }

After ahove change GetPublic() returns 4 instead of 1.

Is there an option to filter these fields that GetFields() still returns 1?





Aucun commentaire:

Enregistrer un commentaire