4

B这里是示例代码,我定义了两个类。如何使用该Output函数输出两个不同类中具有相同名称的成员?

class A
{
    public string Name { get; set; }
    public int Age { get; set; }
    public string Email { get; set; }

    public A(string name, int age, string email)
    {
        Name = name;
        Age = age;
        Email = email;
    }
}

class B
{
    public string Name { get; set; }
    public int Age { get; set; }
    public string Location { get; set; }

    public B(string name, int age, string location)
    {
        Name = name;
        Age = age;
        Location = location;
    }
}

void Output(object obj)
{
    // How can I convert the object 'obj' to class A or class B
    // in order to output its 'Name' and 'Age'
    Console.WriteLine((A)obj.Name); // If I pass a class B in pararmeter, output error.
}
4

7 回答 7

5

您必须:

  1. 使用反射获取属性(如果不存在则抛出)
  2. 有一个公共接口,例如INamed有一个字符串Name属性,这两个类中的每一个都实现了
  3. 声明一个局部变量 asdynamic并使用它来访问Name属性(但实际上这与 #1 相同,因为动态调度将仅使用反射来获取Name属性)。
于 2013-04-07T07:57:56.747 回答
5

您可以利用dynamic绕过编译器,它会在运行时检查类型,因此您不需要强制转换:

void Output(dynamic obj)
{
    Console.WriteLine(obj.Name); 
}
于 2013-04-07T07:58:53.403 回答
5

你应该声明一个接口来描述两个类的共同部分:

interface I
{
    string Name { get; set; }
    int Age { get; set; }
}

然后在Aand中实现它B

class A : I
{
    public string Name { get; set; }
    public int Age { get; set; }
    public string Email { get; set; }

    public A(string name, int age, string email)
    {
        Name = name;
        Age = age;
        Email = email;
    }
}

class B : I
{
    public string Name { get; set; }
    public int Age { get; set; }
    public string Location { get; set; }

    public B(string name, int age, string location)
    {
        Name = name;
        Age = age;
        Location = location;
    }
}

并更改您的方法以获取I参数:

void Output(I obj)
{
    Console.WriteLine(obj.Name); 
}
于 2013-04-07T07:59:57.367 回答
2

您可以使用dynamic、反射或继承。或者您可以复制该方法并利用方法重载

void Output(A obj)
{
 // in order to output its 'Name' and 'Age'
 Console.WriteLine(obj.Age);
 Console.WriteLine(obj.Name);
}

void Output(B obj)
{
 // in order to output its 'Name' and 'Age'
 Console.WriteLine(obj.Age);
 Console.WriteLine(obj.Name);
}
于 2013-04-07T08:00:53.743 回答
2

您需要为这两个类创建一个通用接口,以便编译器知道这两个类可以提供某些功能(在您的情况下为名称和年龄):

    interface IHasNameAndAge
    {
        string Name { get; }
        int Age { get; }
    }

    class A : IHasNameAndAge
    {
        public string Name { get; set; }
        public int Age { get; set; }
        string Email { get; set; }

        public A(string name, int age, string email)
        {
            Name = name;
            Age = age;
            Email = email;
        }
    }

    class B : IHasNameAndAge
    {
        public string Name { get; set; }
        public int Age { get; set; }
        string Location { get; set; }

        public A(string name, int age, string location)
        {
            Name = name;
            Age = age;
            Location = location;
        }
    }

    void Output(IHasNameAndAge obj)
    {
        Console.WriteLine(obj.Name + " is " + obj.Age);
    }
于 2013-04-07T08:04:43.450 回答
1

在您的情况下 - B 不会从 A 继承,因此您不能将 B 转换为 A。

将 B 更改为从 A 继承,例如

class B : A
{
...
}

另外-考虑一下您要在 B 类的 A() 方法中完成什么。并且 - 如果您继承,您可能不需要两次声明相同的成员。

于 2013-04-07T07:58:20.910 回答
0

你可以这样做:

if ( obj.GetType() == typeof( A ) )
{
    Console.WriteLine(((A)obj).Name);
}
else if ( obj.GetType() == typeof( B ) )
{
    Console.WriteLine(((B)obj).Name);
}
于 2013-04-07T07:58:59.257 回答