覆盖dll类的属性集

编程入门 行业动态 更新时间:2024-10-23 03:30:03
本文介绍了覆盖dll类的属性集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在项目中使用了数千个封闭的DllClass实例.

I'm using a thousand instances of a closed DllClass in my project.

public sealed class DllClass { public DllClass(); public string DllClassProperty {get; set;} }

DllClassProperty设置使用了1000次,如果在Web.config上设置了参数,我需要覆盖设置的值.

DllClassProperty set is used a thousand of times and I need to override the value set if a parameter is set on Web.config.

我发现此接口 INotifyPropertyChanged ,但是我不能使用它,因为我没有访问该类的权限,也无法扩展它.

I found this Interface INotifyPropertyChanged, but I can't use it, because I don't have access to the class and I can't extend it.

我在想,是否有办法做这样的事情,但是我认为这在C#中是不可能的:

I'm thinking, if there's a way to do something like this, but I think this is not possible in C#:

public class OnSetPropertyListener<DllClass> { public void OnSetProperty(PropertyInfo propertyInfo, DllClass instance) { // set another value, for example: "new value" } }

如何覆盖DllClassProperty中设置的值?有可能吗?

How can I override the value set in DllClassProperty? Is it possible?

推荐答案

令人惊讶的是,有一种 解决问题的方法.它不是很漂亮,但是可以满足您问题的约束.

Surprisingly, there is a way to solve your problem. It's not pretty, but it satisfies the constraints of your question.

第1步:围绕DllClass创建一个包装器,即一个新的类MyDllClassWrapper,该类的行为与DllClass完全相同,除了要实现的更改之外.通常,这是通过重建DllClass的公共接口并将所有操作转发到私有DllClass实例来完成的.

Step 1: Create a wrapper around DllClass, i.e. a new class MyDllClassWrapper, which behaves exactly like DllClass except for the change that you want to implement. This is usually done by rebuilding the public interface of DllClass and just forwarding all operations to a private DllClass instance.

现在,您只需要在当前使用DllClass的任何地方使用MyDllClassWrapper.您在评论中提到您不想更改所有这些调用,所以让它自动化:

Now you just need to use MyDllClassWrapper everywhere where you currently use DllClass. You mentioned in the comments that you don't want to change all those calls, so let's automate that:

第2步:使用 Substitute.Fody 自动在编译后的步骤中,将对DllClass的所有引用替换为对MyDllClassWrapper的引用:

Step 2: Use Substitute.Fody to automatically replace all references to DllClass by references to MyDllClassWrapper in a post-compile step:

[assembly: Substitute(typeof(DllClass), typeof(MyDllClassWrapper))]

但是请注意,由于源代码指向DllClass,而使用MyDllClassWrapper,因此每个阅读您的代码的人都会感到十分困惑.因此,我建议您仅将此技术用作临时解决方法,直到找到时间将对DllClass的所有引用完全替换为对MyDllClassWrapper的引用.

Note, though, that everyone reading your code will be thoroughly confused, since the source code points to DllClass, but MyDllClassWrapper is used instead. Thus, I recommend that you use this technique only as a temporary workaround until you find the time to cleanly replace all references to DllClass with references to MyDllClassWrapper.

更多推荐

覆盖dll类的属性集

本文发布于:2023-11-13 14:30:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1584579.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   dll

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!