1

I need function, that returns {obj type name}.{property name}.{property name}.. For example:

class City {
    public string Name {get;set;}
}

class Country {
    public string Name {get;set;}
    public City MyCity {get;set;}
}

var myCountry = new Country() { 
    Name="Ukraine",
    MyCity = new City() {
        Name = "Kharkov"
    }
}

So my function should return "{Country.Name}" or "{Country.MyCity.Name}" depends on input parameter. What is the way to do it?

4

3 に答える 3

2

.net リフレクションを使用する

http://www.codersource.net/microsoftnet/cbasicstutorials/cnettutorialreflection.aspx

于 2012-09-23T18:26:00.007 に答える
0

要件に関する情報はあまり投稿していませんが、オブジェクトタイプがわかっている場合は、Reflectionを使用する必要がない場合は、次のisようにテストできます。

if(returnCity && myObject is Country) //I'm assuming that the input value is boolean but, you get the idea...
{
    return myObject.City.Name;
}
else
{
    return myObject.Name;
}

ここで、Reflectionを使用する場合は、次の行の中で何かを行うことができます。

public static string GetNameFrom( object myObject )
{
    var t = myObject.GetType();
    if( t == typeof(Country) )
    {
        return ((Country)myObject).City.Name;
    }

    return ((City)myObject).Name;
}

または、より一般的なアプローチ:

static string GetNameFrom( object myObject )
{
    var type = myObject.GetType();
    var city = myObject.GetProperty( "City" );
    if( city != null)
    {
        var cityVal = city.GetValue( myObject, null );
        return (string)cityVal.GetType().GetProperty( "Name" ).GetValue( cityVal, null );
    }

    return (string)type.GetProperty( "Name" ).GetValue( myObject, null );
}
于 2012-09-23T18:34:29.710 に答える
0

IPrintableファサードを作成し、再帰関数Print()を使用します。アイデアをキャッチし、具体的なタスクのコードを変更してみてください。私の例がお役に立てば幸いです。

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

namespace StackOverflow
{
    interface IPrintable
    {
        string Name { get; }
    }

    class City : IPrintable
    {
        public string Name { get; set; }
    }

    class Country : IPrintable
    {
        public string Name { get; set; }
        public City MyCity { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var myCountry = new Country()
            {
                Name = "Ukraine",
                MyCity = new City()
                 {
                     Name = "Kharkov"
                 }
            };

            Console.WriteLine(Print(myCountry, @"{{{0}}}"));
            Console.WriteLine(Print(new City()
            {
                Name = "New-York"
            }, @"{{{0}}}"));
        }

        private static string Print(IPrintable printaleObject, string formatter)
        {
            foreach (var prop in printaleObject.GetType().GetProperties())
            {
                object val = prop.GetValue(printaleObject, null);
                if (val is IPrintable)
                {
                    return String.Format(formatter, printaleObject.Name) + Print((IPrintable)val, formatter);
                }
            }
            return String.Format(formatter, printaleObject.Name);
        }
    }
}
于 2012-09-23T18:40:26.250 に答える