[英]How can I use the same function for different type parameters which have the same member?
BHere is the sample code, I've defined two classes. BHere是示例代码,我已经定义了两个类。 How can I use the
Output
function to output the member which has the same name in two different classes? 如何使用
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.
}
You'd have to either: 你必须要么:
INamed
that has a string Name
property, that each of the two classes implement INamed
,它具有一个字符串Name
属性,即两个类中的每一个都实现 dynamic
and use it to access the Name
property (but in effect this is the same as #1, because the dynamic dispatch will merely use Reflection to get the Name
property). dynamic
并使用它来访问Name
属性(但实际上这与#1相同,因为动态分派仅使用Reflection来获取Name
属性)。 You should declare an interface which will describe the common part of both classes: 您应该声明一个接口,它将描述两个类的公共部分:
interface I
{
string Name { get; set; }
int Age { get; set; }
}
Then implement it in both A
and B
: 然后在
A
和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;
}
}
And change your method to get I
as parameter: 并改变你的方法让
I
作为参数:
void Output(I obj)
{
Console.WriteLine(obj.Name);
}
You could use dynamic
, reflection, or inheritance. 您可以使用
dynamic
,反射或继承。 Or you could just duplicate the method and take advantage of method overloading 或者你可以复制方法并利用方法重载
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);
}
You need to create a common interface for both classes, so the compiler knows that both classes can dliver certain features (Name and age in your case): 您需要为这两个类创建一个公共接口,因此编译器知道这两个类都可以提供某些功能(在您的情况下为Name和age):
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);
}
In your case - B does not inheret from A, so you cannot cast B to A. 在你的情况下 - B不存在于A中,所以你不能将B投射到A.
Change B to inherit from A, like 改变B继承自A,就像
class B : A
{
...
}
Also - consider what are you trying to accomplish in your A() method of B class. 另外 - 考虑一下你在B类的A()方法中想要完成什么。 And - if you inherited, you probably need not same members to be declared twice.
而且 - 如果你继承了,你可能不需要声明两次相同的成员。
You could do: 你可以这样做:
if ( obj.GetType() == typeof( A ) )
{
Console.WriteLine(((A)obj).Name);
}
else if ( obj.GetType() == typeof( B ) )
{
Console.WriteLine(((B)obj).Name);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.