B
B
bullock2018-05-21 10:27:19
.NET
bullock, 2018-05-21 10:27:19

How to return the names of all fields of the class?

How to make a function inside the class that can return the enumerator which will return string'i with the names of all public fields of this class?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
bullock, 2018-05-21
@bullock

Thanks to everyone who gave advice, in the end the following design worked:

public class M2
    {
        public string str { get; set; }
        public List<string> GetNames()
        {
            return GetType().GetProperties().Select(x => x.Name).ToList();
        }
    }

D
Dmitry Eremin, 2018-05-21
@EreminD

Something like this: https://stackoverflow.com/questions/237275/how-can...

Type type = obj.GetType();

    foreach (var f in type.GetFields().Where(f => f.IsPublic)) {
        Console.WriteLine(
            String.Format("Name: {0} Value: {1}", f.Name, f.GetValue(obj));
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question