C#如何访问另一个类中的变量?

时间:2015-05-08 11:02:00

标签: c# class unity3d member

这让我得到了类的名称(“SolarPanel”),但是我想得到名为“Name”的变量的值(第30行的那个)

image

5 个答案:

答案 0 :(得分:2)

像这样定义虚拟财产:

/// <summary>
/// name: Building
/// </summary>
public class Building
{
    /// <summary>
    /// name of building
    /// </summary>
    public virtual string Name
    {
        get
        {
            // default name is class name with spaces between upper letters
            StringBuilder sb = new StringBuilder();
            bool wasUpper = false;
            foreach (char c in this.GetType().Name)
            {
                if (char.IsUpper(c))
                {
                    if (!wasUpper)
                    {
                        sb.Append(' ');
                        wasUpper = true;
                    }
                }
                else
                {
                    wasUpper = false;
                }
                sb.Append(c);
            }
            return sb.ToString().Trim();
        }
    }

    public void Construct()
    {
        string buildingName = this.Name;
        // do some work
    }
}

/// <summary>
/// name: Missile Station
/// </summary>
public class MissileStation : Building { }

/// <summary>
/// name: Radar Station "Buk"
/// </summary>
public class RadarBuk : Building
{
    /// <summary>
    /// overriden building name
    /// </summary>
    public override string Name { get { return @"Radar Station ""Buk"""; } }
}

答案 1 :(得分:1)

你试图进行上传这是件坏事(因为可能有来自Buildings类的其他后代没有这样的字段)。所以,为了安全起见,你有两个选择:

  • 将字段移至父类(即建筑物)
  • 将Construct()方法重写为虚拟方法,以便后代可以改变行为并访问自己的字段

答案 2 :(得分:1)

我假设您正在构建一个继承类的实例,并且您不知道构造中的类型(因此SolarPanel.Name不是一个选项)。

你可以这样做:

this.getType().GetProperty("Name").GetValue(this, null);

但如果您的所有类型都没有Name属性,则应首先检查GetProperty

var prop = this.getType().GetProperty("Name");
string name = "";

if (prop != null)
{
    name = prop.GetValue(this, null);
} 

答案 3 :(得分:0)

以下是Attributes的方法:

[AttributeUsage(AttributeTargets.Class)]
public class NameAttribute : Attribute {
    public readonly string Name;

    public NameAttribute(string name) {
        this.Name = name;
    }
}

public static class Extensions {
    /// <summary>
    /// If existant, returns the name tag for given object, else null
    /// </summary>
    public static string GetNameTag(this object instance) {
        return instance.GetType().GetNameTag();
    }

    /// <summary>
    /// If existant, returns the name tag for given type, else null
    /// </summary>
    public static string GetNameTag(this Type type) {
        object[] names = type.GetCustomAttributes(typeof(NameAttribute), false);
        switch(names.Length) {
            case 0:
                return null;
            case 1:
                return ((NameAttribute)names[0]).Name;
            default:
                throw new FormatException();
        }
    }
}

[Name("Headquater")]
public class Headquater : Building {
    // ...
}

[Name("Solar Panel")]
public class Solarpanel : Building {
    // ...
}

现在,您可以这样做:

object panel = new SolarPanel();
Console.WriteLine(panel.GetNameTag());

Console.WriteLine(typeof(SolarPanel).GetNameTag());

答案 4 :(得分:-1)

我不同意你的班级结构,但我会尽力回答你的问题:

试试这个:

(string)this.GetType().GetField("Name").GetValue(this);