0

Let's say I have this class:

public class Test
{
    public string Attribute1 { get; set; }
    public int Attribute2 { get; set; }
}

and I have object of this class

Test test = new Test();

Now I want to fill this object, but I don't know what attributes are inside. How can I get references for the attributes to fill them? I was thinking about something like this:

        List<object> attrs = GetAttributes<Test>(test);
        foreach (var attr in attrs)
        {
            if(attr.GetType() == typeof(string))
            {
                attr = "filled";
            }
            else
            {
                attr = 1;
            }
        }
        Console.WriteLine(test.Attribute1); //print "filled"
        Console.WriteLine(test.Attribute2); //print 1
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
Banana Cake
  • 1,172
  • 1
  • 12
  • 31
  • `GetProperties`/`GetRuntimeProperties` – JSteward Aug 10 '18 at 19:38
  • This seems like an XY Problem. Why do you think you need to do this? Have you considered using 3rd party libraries that do object mapping for you? What is the use case here... – maccettura Aug 10 '18 at 19:39
  • 2
    Also, you will likely have more luck if you use the actual name of these, which is "Properties" – maccettura Aug 10 '18 at 19:39
  • You might want to consider using an interface. – Stefan Aug 10 '18 at 19:44
  • None of this solves my problem. I want to will the object with data and I don't know nothing about the properties (names, count, types..). GetProperties gives me properties from the class, but I can't fill them. – Banana Cake Aug 10 '18 at 19:49
  • 1
    Have you checked out the `prop.GetValue(foo, null)`? It retrieves the property value. As for many `get` functions, there is also a `setter`, see [msdn](https://msdn.microsoft.com/en-us/library/xb5dd1f1(v=vs.110).aspx) – Stefan Aug 10 '18 at 19:53
  • @BananaCake I understand what you _want_ to do, but knowing _why_ is more important – maccettura Aug 10 '18 at 19:53
  • @BananaCake: Now, as maccettura says; please tell us why you need this. It goes a bit beyond basic techniques so we are just wondering if you are on the right track to fix your actual problem. – Stefan Aug 10 '18 at 19:56
  • @Stefan thank you, that is what i was looking for. I want to create universal method/ class/ library, where you just put class and object and it will automatically fill the object with random data depends on a type. So for example fill(test); I need it for some testing when I have big classes. – Banana Cake Aug 10 '18 at 19:59
  • 3
    important nomenclature thing: these are not "attributes", they are "properties"; "attributes" has another meaning in C#/.NET, so the two terms are not interchangeable – Marc Gravell Aug 10 '18 at 22:03
  • You might find my answer to this question useful (no one liked my answer to the original question, but it addresses your needs somewhat): https://stackoverflow.com/questions/51444920/c-sharp-find-properties-shared-by-two-types-of-the-same-hierarchy/51447132#51447132 – Flydog57 Aug 11 '18 at 00:38

0 Answers0