none
Get class property-without reflection

    Question

  •    class Program
        {  static void Main(string[] args)
            {  BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.NonPublic |  BindingFlags.Instance | BindingFlags.Static;
                foreach (FieldInfo field in typeof(bb).GetFields(bindingFlags))
                { Console.WriteLine(field.Name);  }//output of all property in class bb o/p- a b c <test>
               Console.ReadKey();
                } }
           public class bb
            { public int a,b,c;
               public int test { get; set; }   }

    Here i am trying to check whether variable (a,b,c) and property(test) are present in class bb.
    So above program will give a result list of variable,property in class bb ,can it done without using reflection.
    Is there any alternate way to check property,variable without using reflection.


    Wednesday, April 5, 2017 1:57 PM

Answers

  • My first questions here would be: why? Is there a performance issue? What is the real problem you are trying to solve?

    Reflection is the tool .net provides that allows you to check the metadata of classes (particularly classes not known at compile-time).

    You appears to be asking the question "How do I check metadata of a class without using the tool designed to check metadata".


    • Edited by RJP1973 Wednesday, April 5, 2017 2:12 PM
    • Marked as answer by learner_test Thursday, April 6, 2017 4:29 AM
    Wednesday, April 5, 2017 2:10 PM
  • You can have all the objects in question implement an interface that exposes the properties that you are interested in and have a list of that interface type. This improves performance over reflection and will ensure static verification of type safety of your code.

    william xifaras

    • Marked as answer by learner_test Thursday, April 6, 2017 4:29 AM
    Wednesday, April 5, 2017 3:03 PM
  • Short answer - no. Given an arbitrary object reflection is the only supported way of getting runtime type information.

    On a more architectural level, if you want to ensure an object has certain members then require the object to implement an interface. Then the compiler will enforce the rules for you. But as RJP1973 said, without knowing the problem it's hard to provide solutions.

    Michael Taylor
    http://www.michaeltaylorp3.net

    • Marked as answer by learner_test Thursday, April 6, 2017 4:29 AM
    Wednesday, April 5, 2017 3:45 PM
    Moderator

All replies

  • My first questions here would be: why? Is there a performance issue? What is the real problem you are trying to solve?

    Reflection is the tool .net provides that allows you to check the metadata of classes (particularly classes not known at compile-time).

    You appears to be asking the question "How do I check metadata of a class without using the tool designed to check metadata".


    • Edited by RJP1973 Wednesday, April 5, 2017 2:12 PM
    • Marked as answer by learner_test Thursday, April 6, 2017 4:29 AM
    Wednesday, April 5, 2017 2:10 PM
  • You can have all the objects in question implement an interface that exposes the properties that you are interested in and have a list of that interface type. This improves performance over reflection and will ensure static verification of type safety of your code.

    william xifaras

    • Marked as answer by learner_test Thursday, April 6, 2017 4:29 AM
    Wednesday, April 5, 2017 3:03 PM
  • Short answer - no. Given an arbitrary object reflection is the only supported way of getting runtime type information.

    On a more architectural level, if you want to ensure an object has certain members then require the object to implement an interface. Then the compiler will enforce the rules for you. But as RJP1973 said, without knowing the problem it's hard to provide solutions.

    Michael Taylor
    http://www.michaeltaylorp3.net

    • Marked as answer by learner_test Thursday, April 6, 2017 4:29 AM
    Wednesday, April 5, 2017 3:45 PM
    Moderator
  • Thanks guy for response,

    I just trying to find the limitations of c# tool  and why is this used over other Technic .

     RJP1973 you are right  "How do I check metadata of a class without using the tool designed to check metadata". i am just trying to find any alternate and to understand the concept on reflection.

    interface is something i missed to look into.

    "classes not known at compile-time"-what do you mean by this.

    is the generic class .

    Thursday, April 6, 2017 4:29 AM

  • "classes not known at compile-time"-what do you mean by this.

    is the generic class .

    I just meant that, if you are writing code that uses a known class - say a class called MyClass with a property A - then you obviously just use it directly: You create an instance MyClass and you access the A property. This is the way you will write the majority of your code.

    You only need to use reflection if you are writing very general code where you don't know anything about the classes that it may need to invoke/use in some way.

    For example, the XmlSerializer (an existing .Net class that produces XML for a class) doesn't know anything about the classes it may have to serialize. It could be passed anything. The only way it can work is to use reflection to analyse the class that it needs to serialize.

    So again, it comes back to the problem you are really trying to solve: If you have a selection of classes that are under your control, which you want to have some kind of shared functionality, then you could create an interface and implement that interface on each of those classes (no reflection required).


    • Edited by RJP1973 Thursday, April 6, 2017 8:40 AM
    Thursday, April 6, 2017 8:40 AM