问题描述
这是一个有点令人费解的问题,希望我能说清楚。
This is a bit of a convoluted question, hopefully I can make it clear.
我发现,这是不可能的,但我想看看是否有人有一个解决方案。
I am finding that this may not be possible, but am trying to see if anybody has a solution.
我有四个班,有两个核心类和两个是那些核心类扩展:
I have four classes, two are core classes and two are those core classes extended:
extUser
扩展 coreUser
extSecurity
扩展 coreSecurity
extUser
Extends coreUser
extSecurity
Extends coreSecurity
在构造 coreUser
你有这样的:
public coreUser(string id, ref coreSecurity cs)
在试图延长 coreUser
你有这样的:
When trying to extend coreUser
you would have this:
public extUser(string id ref extSecurity es) : base(id, ref es)
这将失败,因为ES是类型, extSecurity
和基类需要一个类型 coreSecurity
的。我还没有发现反正投这个,让我重写在C#这个基类。在VB中它工作得很好。
This fails because es is of type, extSecurity
and the base class expects a type of coreSecurity
. I've not found anyway to cast this to allow for me to override this base class in C#. In VB it works just fine.
作为一个说明,我,为了这个问题,100%无法更改的核心类。
As a note, I am, for the sake of this question, 100% unable to make changes to the core classes.
想法?
推荐答案
这是因为参数对 REF参数具有的完全的同键入作为参数,而不仅仅是一个具有转换可用的
This is because arguments for ref
parameters have to have exactly the same type as the parameter, not just one that has a conversion available.
你真的,的真正的需要 REF
部分?制作一个构造采取 REF
参数是pretty的不同寻常。你确定这是不是只是有点混乱参数传递机制的呢?我知道你说,你不能改变的核心的类型,但你至少应该看看这些变化在未来的某个时候,除非他们真正使用这个裁判的功能。如果他们的是的利用裁判的话很有道理,这是不允许的...... coreUser
构造可以是这样的:
Do you really, really need the ref
part? Making a constructor take a ref
parameter is pretty unusual. Are you sure this isn't just a bit of confusion over parameter passing mechanisms? I know you've said that you can't change the "core" types, but you should at least look at changing them at some point in the future unless they're genuinely using this "ref" functionality. If they are making use of "ref" then it makes sense that it's not allowed... the coreUser
constructor could look like this:
public coreUser(string id, ref coreSecurity cs)
{
coreSecurity = new coreSecurity();
}
这会混淆你的 extUser
的构造,不是吗? ES
然后将引用一个对象,它不是一个 extSecurity
...
That would confuse your extUser
constructor, wouldn't it? es
would then refer to an object which wasn't an extSecurity
...
(按照类似的那怪异的线 - ?是你的类型真的驼峰的.NET约定的类型进行PascalCased ...)
(Along similar "that's weird" lines - are your types really camelCased? The .NET conventions are for types to be PascalCased...)
如果可以的真正的唯一修改 extUser
的构造函数,那么作为奥德说,你可以更改参数类型 coreSecurity
- 然后,如果你需要它作为一个 extSecurity
,你可以丢:
If you can really only change the extUser
constructor, then as Oded says you can change the parameter type to coreSecurity
- and then if you need it as an extSecurity
, you cancast it:
public extUser(string id, ref coreSecurity cs) : base(id, ref cs)
{
extSecurity es = (extSecurity) cs;
// Use es here
}
当然,这会如果 CS
指的是一个对象,它不是一个 extSecurity
通过抛出一个异常当底座构造函数返回...
Of course, that will throw an exception if cs
refers to an object which isn't an extSecurity
by the time the base constructor has returned...
这篇关于传递派生对象在建设浅析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!