2

Item.cs

public class Item
{
    public int itemId;
    public string itemName;
    public string itemDescription;
    public string itemType;
}

Food.cs

public class Food
{
    public Item Apple;
    public Item Plum;
}

Im trying this:

Food food = getFood();
string wantedObj = getObjName();

print(food." wantedObj ".itemDescription);

So is it possible to do something like this? I don't know wantedObj, it could be Apple or Plum, so we need to get info depends on wantedObj string.

I don't know how to find class object by object name.

Mazhajk
  • 23
  • 2
  • 2
    Not sure what the end goal is here – Jabberwocky May 20 '20 at 12:23
  • Have `Food` _not_ have properties "Apple" and "Plum" but a `Dictionary`, where the "wantedObj" is the key. – Fildor May 20 '20 at 12:25
  • ^^ see [Dictionary](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2?view=netcore-3.1) – Fildor May 20 '20 at 12:31
  • 7
    I get the feeling you should be using polymorphism instead. This is an XY problem; you're trying to solve some problem and have come up with this bad solution that doesn't work, so you're now asking us to fix your broken solution.. really what we should be doing is helping you with a better solution and throw this one away. Tell us more about your actual problem – Caius Jard May 20 '20 at 12:32
  • Does this answer your question? [Finding the Concrete Type behind an Interface instance](https://stackoverflow.com/questions/1159906/finding-the-concrete-type-behind-an-interface-instance) I think the second answer is the correct one. – Troy Turley May 20 '20 at 12:42
  • @TroyTurley Why do you think the questions are even remotely related? – Fildor May 20 '20 at 12:44
  • why do you think about my anwser? – Jakub Kozera May 20 '20 at 12:51

1 Answers1

0

Consider the following code, without reflection:


using System;
using System.Collections.Generic;

namespace TestAPI
{
    public class Item
    {
        public int itemId;
        public string itemName;
        public string itemDescription;
        public string itemType;
    }

    public class Food
    {
        public Item Apple;
        public Item Plum;
    }

    public class Program
    {
        public static Dictionary<string, Func<Food, Item>> ItemSelectors = new Dictionary<string, Func<Food, Item>>()
        {
            {"Plum", f => f.Plum },
            {"Apple", f => f.Apple },
        };

        public static void Main(string[] args)
        {
            Food food = new Food()
            {
                Apple = new Item()
                {
                    itemId = 1,
                    itemDescription = "Apple",
                    itemName = "Apple"
                },
                Plum = new Item()
                {
                    itemId = 2,
                    itemDescription = "Plum",
                    itemName = "Plum"
                },
            };

            var wantedObj = "Plum";

            Item wantedItem = ItemSelectors[wantedObj](food);

            Console.WriteLine(wantedItem.itemName);

        }
    }
}

Jakub Kozera
  • 3,141
  • 1
  • 13
  • 23