如何将静态类包装在非静态实例对象中(动态)
我有一个有趣的问题。我需要动态包装静态类。即向我的调用者返回一个非静态实例。例如:
public object CreateInstance(string className) {
Type t = assembly.GetType(className);
if (IsStatic(t)) {
return CreateStaticWrapper(t);
} else {
return Activator.CreateInstance(t);
}
}
所以我需要的是如何实现 CreateStaticWrapper 的指针。
注意:不幸的是我无法使用动态对象。
那么我有什么选择呢?我不太热衷于学习IL生成?如果 IL 生成(Reflection.Emit,或者现在还有其他方法吗?)是可行的方法,有人在那里有指针吗?
编辑:值得注意的是,我可以返回代表字典。因此,我可以使用 Delegate.CreateDelegate
来实现此目的,但我似乎无法弄清楚如何处理重载方法和通用方法。
Edit2:另一种选择是使用 Emit 将空构造函数注入到类型中,同样有任何指针吗?这对于标记为静态的类型是否可能? static 关键字是否包含在 IL 中?
Edit3:对于一些上下文,我将其传递到 javascript 环境,请参阅:我的项目 。所以我希望能够(用 JavaScript):
var fileHelper = .create('System.IO.File');
if (fileHelper.Exists(fileName)) { fileHelper.Delete(fileName); }
谢谢大家。
I have an interesting problem. I need to wrap static classes dynamically. I.e. return a non-static instance to my callers. e.g.:
public object CreateInstance(string className) {
Type t = assembly.GetType(className);
if (IsStatic(t)) {
return CreateStaticWrapper(t);
} else {
return Activator.CreateInstance(t);
}
}
So what I need is pointers as to how to implement CreateStaticWrapper
.
Note: I cannot use Dynamic objects unfortunately.
So what are my options? I'm not that keen on learning IL generation? If IL generation (Reflection.Emit, or is there other ways now?) is the way to go does anyone have pointers there?
Edit: It's important to note that I can return a Dictionary of Delegates. So I could use Delegate.CreateDelegate
for this but I can't seem to work out how to handle overloaded methods and Generic methods.
Edit2: Another option would be to inject an empty constructor into the type using Emit, again any pointers? Is this even possible on a type marked as static? Does the static keyword make it into the IL?
Edit3: For a bit of context, I'm passing this to a javascript environment see: my project. So I would like to be able to (in JavaScript):
var fileHelper = .create('System.IO.File');
if (fileHelper.Exists(fileName)) { fileHelper.Delete(fileName); }
Thanks All.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试创建一个继承自 System.Dynamic.DynamicObject 的包装类。在包装类中,使用反射来调用静态类的方法。
你需要这样的东西:
希望它有效。
Try creating a wrapper class which inherits from
System.Dynamic.DynamicObject
. In the wrapper class, use reflection to call the methods of the static class.You need something like this:
Hope it works.
我想说的是IL一代。创建代理是一个非常简单的场景。我实际上写了一篇关于它的博客文章:einarwh。 Posterous.com/patching-polymorphic-pain-at-runtime。场景不同,但解决方案几乎相同。
您基本上可以按照博客文章中的方式进行操作,只是您不需要将“this”引用加载到堆栈上(因为您正在执行静态方法调用)。
I'd say go for IL generation. Creating a proxy is a pretty simple scenario. I actually wrote a blog post about it: einarwh.posterous.com/patching-polymorphic-pain-at-runtime. The scenario is different, but the solution almost identical.
You can basically do exactly as in the blog post, except you don't need to load the 'this' reference onto the stack (since you're doing static method calls).
好吧,我想出的解决方案如下,通过阅读和研究 Einar 的博客文章,他在上面作为评论发布。谢谢艾纳尔。
但我想我应该发布我的 完整内容此处的代码解决方案以防将来可能对某人有所帮助:
Ok, well the solution I've come up with is as follows and was found reading through and studying Einar's blog post which he posted as a comment above. Thanks Einar.
But I thought I'd post my full code solution here in case it may help someone in the future:
因此,假设我们使用“Delegate.CreateDelegate”方式。让我们看看在那之后我们是否可以获得有关您的其他问题的更多详细信息...让我们开始:
静态类是“密封的”,因此不能被继承。所以我不明白你所说的“超载”是什么意思。对于泛型方法,我们需要在将其添加到字典之前调用
methodInfo.MakeGenericMethod(...)
。但是你需要事先知道类型,我猜你不知道......或者,你可以这样做:这会给你一个委托,它将接受一个类型数组(泛型类型参数),并产生一名工作代表。
So, say that we play around with the "Delegate.CreateDelegate" way. And let's see if we can get more details about your other issues after that... Let's start with:
Static classes are 'sealed' and can thus not be inherited. So I don't see what you mean by 'overloaded'. For generic methods, we need to invoke the
methodInfo.MakeGenericMethod(...)
before adding it to our dictionary. But then you would need to know the type beforehand, which I guess you don't... Alternatively, you can do something like:That would give you a delegate that would take a type array (the generic type parameters), and produce a working delegate from that.