简体   繁体   English

如何扩展密封类属性

[英]How to Extend a Sealed Class Property

Suppose you have in a compiled file (dll) a class that is sealed and this class has a public property that is read-only like this one: 假设您在编译文件(dll)中有一个密封的类,并且该类具有一个只读的公共属性,如下所示:

public sealed class MyClass
{
    public MyClass();

    public Guid id { get; }
}

and then you add a reference to your project. 然后添加对您的项目的引用。

My question is: Is there any way to add to the Property id a setter? 我的问题是:有什么方法可以将设置器添加到Property ID?

What I really need is to assign a value to this property. 我真正需要的是为此属性分配一个值。

Let me explain this a little bit more. 让我解释一下。 We have an independent web app (A) that uses two dlls which to operate well makes web service requests to an external app (B). 我们有一个独立的Web应用程序(A),该应用程序使用两个运行良好的dll发出对外部应用程序(B)的Web服务请求。 The App B has 2 different dlls that almost contains the same classes. App B具有2个几乎包含相同类的不同dll。 And the app A is using both dlls to make the requests. 应用程序A使用两个dll发出请求。 Now we are creating a new library for those classes that are “compatibles” each other (or at least have the same name) so that we have only one class to use. 现在,我们为彼此“兼容”(或至少具有相同名称)的类创建一个新的库,以便仅使用一个类。 Now, I saw that in one dll this class MyClass has the property id read/write and, on the other hand, the other dll has the property just as read-only as I showed you earlier. 现在,我看到在一个dll中,该类MyClass的属性ID为read / write,另一方面,另一个DLL的属性与我先前向您展示的一样只读。

First disassemble the dll with reflector or similar. 首先用反射器或类似工具反汇编dll。 Then work out how the getter works. 然后找出吸气剂的工作原理。 With a bit of luck it's reading a field and nothing else, but if it's doing anything more complicated then you'll have more to do in creating a setter. 幸运的是,它正在读取一个字段,但没有其他内容,但是如果它做的事情更复杂,那么在创建二传手中您将有更多工作要做。 Lets say it is indeed a field and that field is called _privateGuidField . 可以说它确实是一个字段,该字段称为_privateGuidField

The create a class that defines an extension method SetId(this MyClass obj, Guid newID) which gets the FieldInfo for that field and calls SetValue on it. 创建一个类,该类定义扩展方法SetId(this MyClass obj, Guid newID) ,该方法获取该FieldInfo并在其上调用SetValue

This will only work with fully-trusted code. 这仅适用于完全信任的代码。

Then sit back and wait for the class whose code-guaranteed invariant you've just smashed apart to misbehave in a really weird way. 然后坐下来等待类,您刚刚将其保证代码的不变式粉碎了,以一种非常奇怪的方式进行了操作。 Don't complain to the people who created the class if it does, because you've just torn their code apart so there's no point complaining if it no longer works. 如果创建了该类,请不要向创建它的人抱怨,因为您只是将他们的代码撕裂了,所以没有必要抱怨它是否不再起作用。

There is no legal way of doing it: the class is sealed, and the property is read-only. 没有合法的方法:密封该类,并且该属性为只读。 If the class implements an interface with the id property, you could wrap that class into yours, and provide a getter and a setter. 如果该类使用id属性实现接口,则可以将该类包装到您的类中,并提供getter和setter。 As it stands, however, your only choice is hacking through reflection, and it's not a valid choice at all. 从目前的情况来看,您唯一的选择是通过反思进行黑客攻击,这根本不是一个有效的选择。 Depending on how the property is implemented, it may be impossible even with reflection. 根据属性的实现方式,即使进行反射也可能无法实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM