不寻常的反思问题 [英] An unusual reflection question

查看:68
本文介绍了不寻常的反思问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一个抽象基类 Base ,它暴露了几个受保护的构造函数Base(),Base(string)和Base(int)。

Imagine an abstract base class Base that exposes several protected constructors Base(), Base(string) and Base(int).

然后想象来自 Base 的派生类,名为 Derived ,它实现了构造函数Derived():base()和Derived(string):base(string)但是
派生(int):base(int)。

Then imagine a derived class from Base called Derived that implements constructors Derived():base() and Derived(string):base(string) but not Derived(int):base(int).

是否可以使用反射一般创建派生
的实例为如果有 Derived (int):base(int)?

Is it possible using reflection to generically create an instance of Derived as if there were a Derived(int):base(int)?

创建方法为T CreateInstance< T>(int)其中T:基础

The create method being T CreateInstance<T>(int) where T : Base

在每个派生构造函数中,除了调用相应的基础构造函数之外,还有无处理

In each of the Derived constructors there is no processing other than invoking the corresponding base constructor.

我的代码适用于由 Derived
实现的所有构造函数,但我还需要以某种方式"假装" Derived 也实现了Derived(int):base(int)。我的代码使用反射来获取相关的Derived构造函数然后调用它,但是如果构造函数实际上没有在
Derived 中实现(但存在于基础中)我没有尝试过(但是)

My code works fine for all constructors that are implemented by Derived but I need to somehow also "pretend" that Derived also implements Derived(int):base(int) too. My code uses reflection to get the relevant Derived constructor and then calls it but in the case of a constructor not actually implemented in Derived (but present in the base) I haven't tried (yet).

如果这是不可能,FormatterServices.GetUninitializedObject()是否有用?

If this is not possible, might FormatterServices.GetUninitializedObject() be of use?

推荐答案

我还没试过它要么,但我认为它应该工作。你为什么不尝试看看......
I haven't tried it either, but I think it should work. Why don't you try it and see ....


这篇关于不寻常的反思问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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