如何处理要扩展其密封在.NET库中的类? [英] How to handle a class you want to extend which is sealed in the .NET library?

查看:115
本文介绍了如何处理要扩展其密封在.NET库中的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在读书的地方如何处理想在.NET Framework库扩展密封类的问题。

这往往有一个共同的和有用的任务做了,所以让我思考,在这种情况下,有什么解决办法?我相信有一个方法证明延伸。在文章中,我读了密封类,但我不记得现在(它不是扩展方法)。

有没有其他办法? 谢谢

解决方案

有假的继承。也就是说,要实现的基类,任何接口的其他类实现:

  //由于
密封类SealedClass:BaseClass的,IDoSomething {}

// 创建
类MyNewClass:BaseClass的,IDoSomething {}
 

您然后有一个私有成员,我通常把它叫做_backing,是这样的:

 类MyNewClass:BaseClass的,IDoSomething
{
   SealedClass _backing =新SealedClass();
}
 

这显然不会与签名等方法的工作原理

 无效NoRefactoringPlease(SealedClass参数){}
 

如果你想扩展自ContextBoundObject继承在某些时候类,看一看的这篇文章。前半部分是COM,第二.NET。它解释了如何代理方法。

除此之外,我想不出任何东西。

I was reading somewhere about how to handle the issue of wanting to extend a sealed class in the .NET Framework library.

This is often a common and useful task to do, so it got me thinking, in this case, what solutions are there? I believe there was a "method" demonstrated to extend a sealed class in the article I read, but I cannot remember now (it wasn't extension methods).

Is there any other way? Thanks

解决方案

There is 'fake' inheritance. That is, you implement the base class and any interfaces the other class implements:

// Given
sealed class SealedClass : BaseClass, IDoSomething { }

// Create
class MyNewClass : BaseClass, IDoSomething { }

You then have a private member, I usually call it _backing, thus:

class MyNewClass : BaseClass, IDoSomething
{
   SealedClass _backing = new SealedClass();
}

This obviously won't work for methods with signatures such as:

void NoRefactoringPlease(SealedClass parameter) { }

If the class you want to extend inherits from ContextBoundObject at some point, take a look at this article. The first half is COM, the second .Net. It explains how you can proxy methods.

Other than that, I can't think of anything.

这篇关于如何处理要扩展其密封在.NET库中的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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