0

This is an extension of a question I had beforehand

I have a specific function that I want to run, and it is located inside an XML File:

Console.WriteLine("Text for test, {0}, {1}", testWord, testWord2);

The text is stored in an XML file:

<?xml version="1.0" encoding="utf-8" ?>
<root>
  <world>
    <region name="TestRegion">
      <area name="TestArea">
        <building name="Outside">
          <room name="TutorialRoom">
            <textToDisplay>"Text for test, {0},{1}"</textToDisplay>
            <extraString>testWord,tesWord2</extraString>
          </room>
        </building>
      </area>
    </region>
  </world>
</root>

I can easily get the string data using LINQ

XElement xelement = XElement.Load("..\\..\\LocationDatabase.xml");

        var textToDisplay= xelement.Elements("world")
            .Elements("region").Where(region => (string)region.Attribute("name") == "TestRegion")
            .Elements("area").Where(area => (string)area.Attribute("name") == "TestArea")
            .Elements("building").Where(building => (string)building.Attribute("name") == "Outside")
            .Elements("room").Where(room => (string)room.Attribute("name") == "TutorialRoom")
            .Elements("textToDisplay");

        var extraString= xelement.Elements("world")
            .Elements("region").Where(region => (string)region.Attribute("name") == "TestRegion")
            .Elements("area").Where(area => (string)area.Attribute("name") == "TestArea")
            .Elements("building").Where(building => (string)building.Attribute("name") == "Outside")
            .Elements("room").Where(room => (string)room.Attribute("name") == "TutorialRoom")
            .Elements("extraString");

And this works completely fine. The issue I have is when I don't have a word in the XML file, but rather a property of a class. I have a singleton Player, and it has a autoproperty Name. To normally access it, I can just say:

Console.WriteLine("Your name is:", Player.Instance.Name);

But how do I, instead, keep this in the XML file? Like:

<?xml version="1.0" encoding="utf-8" ?>
<root>
  <world>
    <region name="TestRegion">
      <area name="TestArea">
        <building name="Outside">
          <room name="TutorialRoom">
            <textToDisplay>"Your name is: {0}"</textToDisplay>
            <extraString>Player.Instance.Name</extraString>
          </room>
        </building>
      </area>
    </region>
  </world>
</root>

When I use the past command, it simple thinks that whole section is a string, and outputs "Your name is: Player.Instance.Name"

An example using my own code: enter image description here

The Player Class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GuardsOfAetheria
{
    class Player
    {
        public enum Class
        {
            Melee,
            Magic,
            Ranged
        }
        public string Name { get; set; }
        public Class PlayerClass { get; set; }

        private static readonly Player instance = new Player();

        static Player()
        {
        }

        private Player()
        {
        }

        public static Player Instance
        {
            get
            {
                return instance;
            }
        }
    }
}

Is there a way to solve this?

EDIT 1: I was able to do something similar using the following lines of code:

var typ = typeof(Player);
var prop = typ.GetProperty("Name");
var propVal = prop.GetValue(Player.Instance);

Console.WriteLine(((string)textToDisplay.First()).Replace(@"\n", Environment.NewLine), propVal);

This works fine, and gets the necessary data. The issue here is that in different parts, different classes have to be called var typ = typeof(Player), and different instances have to be attributed var propVal = prop.GetValue(Player.Instance). I can store the name of the class and instance I need to get from, but simply using a string that holds that data doesn't work, like below:

string className = "Player";

var typ = typeof(className);
var prop = typ.GetProperty("Name");
var propVal = prop.GetValue(Player.Instance);

Console.WriteLine(((string)textToDisplay.First()).Replace(@"\n", Environment.NewLine), propVal);

Is there anyway to do that?

1 Answers1

0
Type player = Type.GetType("Player");

or

Type player = Type.GetType("myNamespace.Player");

See here for more info

Then you are going to have to get an instance of that type, see manipulating types. Then you will need to get the PropertyInfo's for the type, then get the one you want Instance, etc for Instance object.

You will need to get into intimate detail of telling the compiler what object your working with and what properties you want to access. Reflection is a runtime compiled way of accessing objects. It is definitely more complicated than the run-of-the-mill program.

Community
  • 1
  • 1
Chuck Savage
  • 11,775
  • 6
  • 49
  • 69