如何将链式静态类名和属性值转换为字符串?

时间:2010-10-07 16:32:00

标签: c#

上下文 假设一个具有以下类结构:

public static class SomeStaticClass
{
    public static class SomeInnerStaticClass
    {
        public static readonly string SomeProperty = "someStringValue";
    }
}

问题:

有没有一种简单的方法可以将引用SomeStaticClass.SomeInnerStaticClass.SomeProperty转换为字符串值“SomeStaticClass.SomeInnerStaticClass.someStringValue”?

5 个答案:

答案 0 :(得分:3)

我发布的第一件事是错误的,因为它们是静态类型我写了一些代码,这是有效的。

public static class A
{
    public static class B
    {
        public static string c
        {
            get
            {
                return "hi";
            }
        }
    }

}
class Program
{
    static void Main( string[] args )
    {

        Console.WriteLine(typeof(A.B).FullName.Replace("+",".") + "." +  A.B.c  ) ;
    }


}

答案 1 :(得分:1)

你不只是想要:

public static class SClass
{
   public static class SInner
   {
       public static string Property = 
            (typeof(SInner)).DeclaringType.Name+ "." 
            + typeof(SInner).Name + "."
            + "value";
   }
}

哪会输出SClass.SInner.Property.value

如果你想自动化它,可以将它放在while循环中,并在父类型属性IsNested为假时退出。

答案 2 :(得分:1)

您可以使用表达式:

namespace ConsoleApplication2
{
    using System;
    using System.Linq.Expressions;

    class Program
    {
        static void Main(string[] args)
        {
            string fullName = GetExpression(() => SomeClass.SomeProperty);

            Console.WriteLine(fullName);
        }

        public static string GetExpression<TProperty>(Expression<Func<TProperty>> expr)
        {
            string name = expr.Body.ToString();
            string value = expr.Compile().Invoke().ToString();

            name = name.Substring(0, name.LastIndexOf(".") + 1) + value;

            return name;
        }
    }

    public static class SomeClass
    {
        public static string SomeProperty = "Hello";
    }
}

答案 3 :(得分:1)

这是一个更精细的解决方案。该方法接受表示属性或字段的lambda表达式。它计算属性或字段的值,并在其前面加上所有封装类的名称。

我想你想要一种类型安全的方法来生成某种结构化/分层标识符,这些标识符被编码为类和属性或字段的层次结构。我已经使用这种方法几年了,它有助于防止由简单的错别字导致的许多难以捕获的错误。

代码:

public string GetStructuredName(Expression<Func<object>> nameObject)
{
    if (nameObject == null) throw new ArgumentNullException("nameObject");

    // find the expression denoting a property or a field
    MemberExpression member = null;
    switch (nameObject.Body.NodeType)
    {
        case ExpressionType.MemberAccess:
            member = (MemberExpression)nameObject.Body;
            break;
        case ExpressionType.Convert:
        case ExpressionType.ConvertChecked:
            member = ((UnaryExpression)nameObject.Body).Operand as MemberExpression;
            break;
    }
    if (member == null) throw new ArgumentNullException("nameObject");

    StringBuilder sb = new StringBuilder();

    // use the value of the member as the final component of the resulting name
    sb.Append(nameObject.Compile().DynamicInvoke());

    // use short names of embedded type names as components of the resulting name
    Type type = member.Member.DeclaringType;
    while (type != null && type != typeof(Object))
    {
        sb.Insert(0, ".");
        sb.Insert(0, type.Name);
        type = type.DeclaringType;
    }

    return sb.ToString();
}

示例:

public class OuterContainer
{
    public class InnerContainer
    {
        public static string Property
        {
            get { return "value"; }
        }

        public static int Field = 10;
    }
}

GetStructuredName(() => OuterContainer.InnerContainer.Property)
GetStructuredName(() => OuterContainer.InnerContainer.Field)

输出将是:

"OuterContainer.InnerContainer.value"
"OuterContainer.InnerContainer.10"

答案 4 :(得分:0)

我不知道输入格式的性质,但您可以使用 System.Linq.Expressions 执行此操作:

    Expression<Func<string>> expr = () =>
        SomeStaticClass.SomeInnerStaticClass.SomeProperty;

    var myResult = String.Concat(expr.Body, ".", expr.Compile().Invoke());
相关问题