Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 试图获取表示类继承人身份的字符串_C# - Fatal编程技术网

C# 试图获取表示类继承人身份的字符串

C# 试图获取表示类继承人身份的字符串,c#,C#,我希望能够为B的一个实例获取字符串Base.A.B 我当然可以这么做 class Base { } class A: Base { } class B:A { } 但这有点脆弱,如果我改变了事情,我必须在很多地方改变它 我从 class B:A { const string NAME = "Base.A.B"; } 每个类层次结构级别的dea调用其base.Name方法。但我现在不得不再次为Base命名两次,承认如果我忘记了,它将在编译时失败,但它看起来仍然很脆弱。这样如何: clas

我希望能够为B的一个实例获取字符串Base.A.B

我当然可以这么做

class Base
{
}
class A: Base
{
}
class B:A
{
}
但这有点脆弱,如果我改变了事情,我必须在很多地方改变它

我从

class B:A
{
   const string NAME = "Base.A.B";
}
每个类层次结构级别的dea调用其base.Name方法。但我现在不得不再次为Base命名两次,承认如果我忘记了,它将在编译时失败,但它看起来仍然很脆弱。

这样如何:

class Base
{
protected string Name {get{return typeof(Base).Name;}};
}
只是一个想法:

这个怎么样:

class Base
{
protected string Name {get{return typeof(Base).Name;}};
}
只是一个想法:

这种方法:

class Base {
   public virtual string GetName() { return "Base"; }
}

class A : Base {
   public override string GetName() { return base.GetName() + ".A"; }
}

class B : A  {
   public override string GetName() { return base.GetName() + ".B"; }
}
当这样称呼时:

public static string GetHierarchy(Type t)
{
    if (t == null)
    {
        return "";
    }

    string rec = GetHierarchy(t.BaseType);

    return rec + "." + t.Name.ToString();
}
将产生以下结果:

string str = GetHierarchy(typeof(B));
编辑以防止NullReferenceException

此方法:

class Base {
   public virtual string GetName() { return "Base"; }
}

class A : Base {
   public override string GetName() { return base.GetName() + ".A"; }
}

class B : A  {
   public override string GetName() { return base.GetName() + ".B"; }
}
当这样称呼时:

public static string GetHierarchy(Type t)
{
    if (t == null)
    {
        return "";
    }

    string rec = GetHierarchy(t.BaseType);

    return rec + "." + t.Name.ToString();
}
将产生以下结果:

string str = GetHierarchy(typeof(B));

编辑以防止零引用异常

您可以考虑使用反射。

您可以像这样注释类

".Object.Base.A.B"
然后您可以使用例如:

[Name("Base")]
public class Base { }

[Name("A:Base")]
public class A : Base { }

[Name("B:A:Base")]
public class B : A { }

这给了你TAF[0 ]=B: A:Base< /P> < P>你可以考虑使用反射。

您可以像这样注释类

".Object.Base.A.B"
然后您可以使用例如:

[Name("Base")]
public class Base { }

[Name("A:Base")]
public class A : Base { }

[Name("B:A:Base")]
public class B : A { }

这就给了你attr[0]==B:A:Base

和其他人一样的代码,但我付出了努力,所以我会很好地发布它。唯一的区别是我不想要。对象被卡住了

System.Attribute[] attrs = System.Attribute.GetCustomAttributes(typeof(B));

和其他人的代码一样,但我已经努力了,所以我会很好地发布它。唯一的区别是我不想要。对象卡在了最后

System.Attribute[] attrs = System.Attribute.GetCustomAttributes(typeof(B));

这是在@Jaime的答案的基础上建立的,所以我会给他大部分的信任,但我修改了它,这样你就不必在其中添加需要记住更新的自定义属性

*您需要添加对System.Reflection的引用

    public class Class1 { }
    public class Class2 : Class1 { }
    public class Class3 : Class2 { }

    private void button1_Click(object sender, EventArgs e)
    {
        Class3 c3 = new Class3();
        Console.WriteLine(TypeToString(c3.GetType()));
    }

    private string TypeToString(Type t)
    {
        if (t == null)
            return "";
        if ((t.BaseType == null) || (t.BaseType == typeof(object)))
            return t.Name;
        return TypeToString(t.BaseType) + "." + t.Name;
    }

这是在@Jaime的答案的基础上建立的,所以我会给他大部分的信任,但我修改了它,这样你就不必在其中添加需要记住更新的自定义属性

*您需要添加对System.Reflection的引用

    public class Class1 { }
    public class Class2 : Class1 { }
    public class Class3 : Class2 { }

    private void button1_Click(object sender, EventArgs e)
    {
        Class3 c3 = new Class3();
        Console.WriteLine(TypeToString(c3.GetType()));
    }

    private string TypeToString(Type t)
    {
        if (t == null)
            return "";
        if ((t.BaseType == null) || (t.BaseType == typeof(object)))
            return t.Name;
        return TypeToString(t.BaseType) + "." + t.Name;
    }

我已经编写了以下扩展方法和递归方法。 我相信这是解决这个问题的好办法

看看吧,任何反馈都很好=

class Base
{
    public virtual string GetName()
    {
        return MethodBase.GetCurrentMethod().DeclaringType.ToString();
    }
}

class A : Base
{
    public override string GetName()
    {
        return base.GetName() + "." + MethodBase.GetCurrentMethod().DeclaringType.ToString();

    }
}

class B : A
{
    public override string GetName()
    {
        return base.GetName() + "." + MethodBase.GetCurrentMethod().DeclaringType.ToString();
    }
}

class C : B
{
    public override string GetName()
    {
        return base.GetName() + "." + MethodBase.GetCurrentMethod().DeclaringType.ToString();
    }
}

C test = new C();
test.GetName(); // Yields "Base.A.B.C"
用法:

public static class FamilyTreeExtension
    {
        public static string GetFamilyTree(this Type t)
        {
            return GetFamilyTreeRecursive(t, t.Name);
        }

        public static string GetFamilyTreeRecursive(Type t, string leaf)
        {
            Type baseType = t.BaseType;

            string currentTree;

            if (baseType != null)
            {
                currentTree = string.Format("{0}.{1}", baseType.Name, leaf);
                return GetFamilyTreeRecursive(baseType, currentTree);
            }
            else
            {
                return leaf;
            }
        }
    }

结果:Object.A.B.C

我编写了以下扩展方法和递归方法。 我相信这是解决这个问题的好办法

看看吧,任何反馈都很好=

class Base
{
    public virtual string GetName()
    {
        return MethodBase.GetCurrentMethod().DeclaringType.ToString();
    }
}

class A : Base
{
    public override string GetName()
    {
        return base.GetName() + "." + MethodBase.GetCurrentMethod().DeclaringType.ToString();

    }
}

class B : A
{
    public override string GetName()
    {
        return base.GetName() + "." + MethodBase.GetCurrentMethod().DeclaringType.ToString();
    }
}

class C : B
{
    public override string GetName()
    {
        return base.GetName() + "." + MethodBase.GetCurrentMethod().DeclaringType.ToString();
    }
}

C test = new C();
test.GetName(); // Yields "Base.A.B.C"
用法:

public static class FamilyTreeExtension
    {
        public static string GetFamilyTree(this Type t)
        {
            return GetFamilyTreeRecursive(t, t.Name);
        }

        public static string GetFamilyTreeRecursive(Type t, string leaf)
        {
            Type baseType = t.BaseType;

            string currentTree;

            if (baseType != null)
            {
                currentTree = string.Format("{0}.{1}", baseType.Name, leaf);
                return GetFamilyTreeRecursive(baseType, currentTree);
            }
            else
            {
                return leaf;
            }
        }
    }

结果:Object.A.B.C

u获胜,因为您的解决方案前面没有符合“规范”的“.Object”。这两个递归答案都会让你赢得业力,因为你的解决方案前面没有“规范”中的“.object”。这两个递归答案都得到了因果报应。我本来打算接受这个答案,直到我看到Michaels的答案,他没有在结果字符串上加上“.object”。但是这两种方法都得到了简洁的解决方案——txi打算接受这个答案,直到我看到Michaels的答案,他没有在结果字符串上加上“.object”。但这两个都得到了干净的解决方案-tx的+