0

May I dont know how too look for it, but fact is I cant find anything that helps me.

Is there a way to call property from a parent class like an assoc array (dictionary) ?

Sample:

using System;

class Foobar
{
    public string bla;
    public Foobar()
    {
        this.bla = "hello world";
    }
}

public class Test
{
    public static void Main()
    {
        Foobar x = new Foobar();
        Console.WriteLine(x.bla); //this works (prints hello world)
        Console.WriteLine(x["bla"]); //this wont work but is my achivment
    }
}

To clarify what I want is...

I want to create a class that has some propertys e.g

class SomeClass
{
    private string aaa {get;set;};
    private string bbb {get;set;};
    private string ccc {get;set;};
    private string ddd {get;set;};
    ....
}

and than loop this in a other class by a dictionary

SomeClass x = new SomeClass();
IDictionary<string, string> dict = new Dictionary<string, string>();
dict["a"] = "aaa";
dict["b"] = "bbb";
dict["d"] = "ddd";

foreach( d in dict )
{
    someMethode(x[d]);
}
Dwza
  • 6,494
  • 6
  • 41
  • 73
  • 1
    Possible duplicate of [How to get a property value using reflection](http://stackoverflow.com/questions/10338018/how-to-get-a-property-value-using-reflection) – ASh Mar 30 '17 at 12:01
  • @ASh its not from the same class.... is the main thing im looking for called reflection ? – Dwza Mar 30 '17 at 12:03
  • First, C# is not php so don't compare it. Second, your research was very bad regarding the topic. Third, what have you searched for so you didn't come up with a solution? Fourth, this doesn't have to do anything with inheritance because you called it 'parent class'. – ckruczek Mar 30 '17 at 12:03
  • @ckruczek of course c# is not php and i didnt expact to compare it. Thats why i not added a php tag. I only know the functionality in php exists. Point 2 and 3, thats exactly my problem. I tried to describe a problem what name I dont know... So i gave my best to describe what I want – Dwza Mar 30 '17 at 12:06
  • `its not from the same class` <= that does not matter, just do `typeof(Foobar)` and you have the type. – Igor Mar 30 '17 at 12:06
  • @Dwza, yes. http://stackoverflow.com/tags/system.reflection/info. Also `attribute` is another special concept in .net – ASh Mar 30 '17 at 12:07
  • @Igor why should i use the type.. i dont need the type ^^ what i want is to access the propertys of the class in a loop – Dwza Mar 30 '17 at 12:08
  • 2
    why do you want something like that? what is your actual requirement? – bansi Mar 30 '17 at 12:08
  • If its only for that one type then use the answer by @fubo. If its for any arbirtrary type then yes, you will need the type if you want to use reflection to get a property or field (NOT attribute which is something different) by name. – Igor Mar 30 '17 at 12:10
  • @fubo, I would say it should better be. public instance fields are against guidelines – ASh Mar 30 '17 at 12:11
  • you should also take a look at [Dynamic Type](https://msdn.microsoft.com/en-us/library/dd264736.aspx) – bansi Mar 30 '17 at 12:25
  • @bansi added some more details to my question for your "why do you want this..." comment. Also i dont need the type :) i corrected my question by changing attribute to property. – Dwza Mar 30 '17 at 13:09

2 Answers2

4

Modify your class this way

class Foobar
{
    public string bla { get; set; }
    public Foobar()
    {
        this.bla = "hello world";
    }

    public string this[string name]
    {
        get
        {
            return this.GetType().GetProperty(name).GetValue(this, null).ToString();
        }
    }
}

Sample: https://dotnetfiddle.net/XboHOg

fubo
  • 44,811
  • 17
  • 103
  • 137
  • actually this looks like exact what I want... is this called reflection ? – Dwza Mar 30 '17 at 12:14
  • 1
    yes, combined with public properties and a indexer https://msdn.microsoft.com/en-us/library/6x16t2tx.aspx – fubo Mar 30 '17 at 12:15
1

You should do it in this way to extend it ^^

using System;


    class Program
    {
        static void Main(string[] args)
        {
            Foobar x = new Foobar();
            Console.WriteLine(x.bla); //this works (prints hello world)
            Console.WriteLine(x["bla"]); //this wont work but is my achivment
        }
    }

    class Foobar : Class
    {
        public Foobar()
        {
            this.bla = "hello world";
        }

        public string bla { get; set; }
    }

    class Class
    {
        public string this[string name]
        {
            get
            {
                return this.GetType().GetProperty(name).GetValue(this).ToString();
            }
        }
    }
Marco Salerno
  • 5,131
  • 2
  • 12
  • 32