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

创建一个 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 回答
0

您没有发布太多有关要求的信息,但是,如果您知道您的对象类型,则无需使用反射,您可以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;
}

现在,如果你想使用反射,你可以在这些行中做一些事情:

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 回答