有没有办法从具有内部构造函数的类派生? [英] Is there a way to derive from a class with an internal constructor?

查看:85
本文介绍了有没有办法从具有内部构造函数的类派生?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用具有很多出色的方法和属性的第三方c#类,但是随着时间的流逝,我需要使用自己的方法和属性来扩展该类。如果是我的代码,我将使用该类作为基类,并在其顶部添加我自己的属性和方法-但是该类具有内部构造函数。 (在我看来,使构造函数内部成为首要对象的时间很短-为什么限制子类的功能?)

I'm working with a 3rd party c# class that has lots of great methods and properties - but as time has gone by I need to extend that class with methods and properties of my own. If it was my code I would just use that class as my base class and add my own properties and method on top - but this class has an internal constructor. (In my opinion it was short sited to make the constructor internal in the first place - why limit the ability to subclass?)

我唯一能想到的就是在我的类上创建方法/属性,只是简单地调用它们的方法-但这是大量的代码,而且,它只是感觉不到。

The only thing I could think of was to create method / properties on my class that simply called into theirs - but it's acres of code and, well, it just doesn't "feel" right.

有没有

推荐答案

我不会讨论您是否可以围绕该第三方类建立自己的Facade 。以前的作者是对的,库的设计方式不允许这样做。假设他们有一些耦合类,这些类具有应按特定顺序初始化的单例或类似的东西-可能有很多第三方开发人员从不关心的设计错误(或功能),因为他们不认为您会使用

I will not discuss whether you can build your own Facade around that 3rd party class. Previous authors are right, the library could be designed in the way that will not allow this. Suppose they have some coupled classes that have singletons that should be initialized in specific order or something like this - there may be a lot of design mistakes (or features) that 3rd party developers never care about, because they do not suppose that you will use their library in that way.

但是好吧,假设构建外墙不是不可能的任务,实际上您只有一个问题-有您必须编写包装器的方法太多,并且手动执行此操作不好。

But OK, lets suppose that building a facade is not an impossible task, and you have in fact only one problem - there are too many methods you have to write wrappers around, and it is not good to do this manually.

我看到3种解决方案来解决该问题

I see 3 solutions to address exactly that problem

1)我认为.NET 4.0的新动态类型将使您能够解决该问题,而不必编写英亩的代码
您应该封装带有动态关键字
的privare成员的第三方类实例进入您的类您的类应从Dynamic派生或实现IDynamicObject接口。您将必须实现GetMember / SetMember函数,该函数会将所有调用转发到第三方类的封装实例

1) I suppose that new "dynamic" types of .NET 4.0 will allow you to workaround that problem without having to write "acres of code" You should incapsulate an instance of 3rd party class into your class as a privare member with dynamic keyword Your class should be derived from Dynamic or implement IDynamicObject interface. You will have to implement GetMember/SetMember functions that will forward all calls to the encapsulated instance of 3rd party class

C#4.0是未来,让我们在其他解决方案上看看:

Well, c# 4.0 is a future, Let's see on other solutions:

2)如果您拥有大量的公共方法(例如,超过100种),请不要手动编写代码。我将编写一个小的控制台应用程序,该应用程序使用反射并查找所有公共成员,然后自动生成代码以调用封装的实例。例如

2) Do not write code manually if you have significant number of public methods (say more then 100). I would write a little console app that uses reflection and finds all public members and then automatically generates code to call encapsulated instance. For example

public type MethodName(params)
{
   this.anInstanceOf3rdPartyClass.MethodName(params);
}

3)您可以执行与2相同的操作,但要借助现有的反射工具,例如RedGate .NET Reflector。它将帮助您列出所有类和方法签名。然后,将所有这些内容粘贴到Word中,然后使用一个简单的VB宏将生成与在2中相同的代码。
备注:一旦不复制代码,而仅复制方法签名, ,这些都是公开可用的,我认为您不会违反许可协议,但是无论如何值得重新检查

3) You can do the same as 2, but with the help of existing reflection tools, for example RedGate .NET Reflector. It will help you to list all classes and methods signatures. Then, paste all this in Word and a simple VB macro will let you generate the same code as you could do in 2. Remark: As soon as you are not copying the code, but only copying method signatures, that are publicly available, I don't think you will violate the license agreement, but anyway it worth to re-check

这篇关于有没有办法从具有内部构造函数的类派生?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆