泛型:访问新成员,而不是隐藏成员 [英] Generics: Accessing New Members, Not Hidden Members
问题描述
我遇到了泛型和新成员的问题。我写了一个泛型类,它对ObjectA类型的对象进行操作。 ObjectB从ObjectA派生并隐藏了一些ObjectA的成员。当我将ObjectB的类型作为类型参数提供给泛型类时,我期望在调用由ObjectB隐藏的任何成员时,我将调用ObjectB的实现。但是,CLR仍然调用隐藏成员(ObjectA的实现)。这似乎不合逻辑,因为我明确提供了泛型类的ObjectB类型。这是泛型本身的问题,还是我做错了什么?
编辑:不幸的是,我没有访问ObjectA的源代码和我想覆盖的成员不是虚拟的。如果我有权访问ObjectA的源代码,我会让这个成员变成虚拟的,但是我不能这么做,我唯一的选择是通过new关键字来覆盖成员。
class GenericClass< T>其中T:ObjectA
{
public void DoWork(T item)
{
//当类型参数'T'是ObjectB时,应该得到ObjectB的实现
item。调用();
$ b class ObjectA
{
public void Invoke()
{
// A的实现...
class ObjectB:ObjectA
{
public new void Invoke()
{
// B的实现...
}
}
static void Main()
{
GenericClass< ObjectB> genericClass = new GenericClass< ObjectB>();
ObjectB objectB = new ObjectB();
genericClass.DoWork(objectB);
}
div class =h2_lin>解决方案
没有。编译器生成的调用是在编译时知道的成员 。这是由 ObjectA
公开的成员。
任何你没有使用正常继承的原因,使用虚拟/重载方法?
这是另一个例子顺便说一句,尽管 T
是 string $ c,但字符串的重载==操作符不被使用在调用
Foo
:
class Test b $ b {
static bool Foo< T>(T first,T second)
where T:class
{
return第一次==第二;
}
static void Main()
{
string x =hello;
string y = new string(x.ToCharArray());
Console.WriteLine(Foo(x,y));
}
}
I have run into a problem with generics and new members. I wrote a generic class which operates on an object of type ObjectA. ObjectB derives from ObjectA and hides a few of ObjectA's members. When I supply the type of ObjectB as the type parameter to the generic class, I would expect that when I call any of the members hidden by ObjectB, I would be calling ObjectB's implementation. However, the CLR still calls the hidden members (ObjectA's implementation). This seems illogical because I explicitly provided the type of ObjectB to the generic class. Is this a problem with generics themselves, or am I doing something wrong?
Edit: Unfortunately, I do not have access to ObjectA's source code and the member I want to override is not virtual. If I had access to ObjectA's source code, I would make the member virtual, but as I cannot do so, my only option for "overriding" the member is through the "new" keyword.
class GenericClass<T> where T : ObjectA
{
public void DoWork(T item)
{
// When type parameter 'T' is ObjectB, should get ObjectB's implementation
item.Invoke();
}
}
class ObjectA
{
public void Invoke()
{
// A's implementation...
}
}
class ObjectB : ObjectA
{
public new void Invoke()
{
// B's implementation...
}
}
static void Main()
{
GenericClass<ObjectB> genericClass = new GenericClass<ObjectB>();
ObjectB objectB = new ObjectB();
genericClass.DoWork(objectB);
}
No. The calls generated by the compiler are to the members it knows about at compile-time. That's the members exposed by ObjectA
.
Any reason you're not using normal inheritance, with virtual/overridden methods?
Here's another example of the same kind of thing, by the way - the overloaded == operator for strings isn't used, even though T
is string
in the call to Foo
:
using System;
class Test
{
static bool Foo<T>(T first, T second)
where T : class
{
return first == second;
}
static void Main()
{
string x = "hello";
string y = new string(x.ToCharArray());
Console.WriteLine(Foo(x, y));
}
}
这篇关于泛型:访问新成员,而不是隐藏成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!