-1

In the list of objects of type

 List<Configuracion.Models.v_cCfgDeclaraciones>

I would like retrive the name field, I would like to find the name of the field within the loop (foreach) better if I do not use reflection, how i can resolve this?

enter image description here

4
  • 2
    To list all the properties of an object you have to resort to reflection. But you do know the type? Then just hard code them as a list of strings? Dec 4, 2013 at 13:53
  • If you just want the names of the fields in the type, where does a loop come in? What are you planning on doing with the names? That might help us answer your question.
    – A.R.
    Dec 4, 2013 at 13:55
  • What are those names? You have supplied a screenshot, rather than code, so it's hard to guess what the code is beneath the overlay. What is the purpose of the switch inside a foreach loop?
    – David Arno
    Dec 4, 2013 at 13:56
  • A.R I want to retrieve the name field because I need compared into the loop, for example if name field is "pog_01varchar" I get a substring to stay with the name "01varchar" I retrieve the value, not value of "pog_02integer" because the value must be greater than 0 and so with other fields that are valid values
    – raranibar
    Dec 4, 2013 at 14:08

1 Answer 1

0

Put together something on Linqpad. Note that this does use reflection:

    void Main() {
        Test t = new Test();
        Type tType = t.GetType();
        foreach (PropertyInfo prop in tType.GetProperties()) {
            prop.Name.Dump();
        }
    }

    public class Test {
        public string prop1 { get; set; }
        public string prop2 { get; set; }
        public Test() {
            prop1 = "Property 1";
            prop2 = "Property 2";
        }
    }

I don't know of a way you can get this information (short of hard-coding it in) without using reflection. Is there a reason you don't want to use reflection in this case?

2
  • I didn't want to use reflection thinking could recover the name of the field as shown in the image. But if not I can as you would with reflection?
    – raranibar
    Dec 4, 2013 at 14:41
  • I think reflection is the suggested way of collecting property names. If you're worried about efficiency, I've found that reflection is actually quite quick. But yes, in the code above, "prop.Name" would be what you're looking for, I believe.
    – dvlsg
    Dec 4, 2013 at 15:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.