实例化一个内部类并将其转换为给定类型 [英] Instantiating an Internal class and casting it as a given type

查看:170
本文介绍了实例化一个内部类并将其转换为给定类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

继续 InternalsVisibleTo 。我已查看 c#使用私有构造函数实例化内部类,有帮助,但我试图把返回的对象作为内部类型,老实说,我不是100%,这是可能的。

Following up on InternalsVisibleTo. I have looked at c# Instantiating Internal class with private constructor, and this has helped but I'm trying to cast the returned object as the internal type and, honestly I'm not 100% that that is possible.

我试图反射路线来解决这个问题,但我有一个艰难的时间试图找出如何实例化内部类型与私人方法使用反射。我可以去拉到类型和获取构造函数和创建一个对象。

I'm trying the route of Reflection to fix this issue, but I'm having a tough time trying to figure out how to instantiate an internal type with private methods using reflection. I can go as far as pulling the type and getting the constructor and creating an object.

如果我想要转换的类型是一个内部类型,我将如何预测对象的转换。

How would I preform the cast of the object if the type I wish to cast is an internal type.?

public object InitPrivateCoreObjects(string Type)
{
    Assembly Core = Assembly.Load("Stuff.Core, Version=0.3.3881.21340, Culture=neutral, PublicKeyToken=4fe470e63e2d354e");
    Type TypeToReflect = Core.GetType("Stuff.Core.AssemblyWithIdentifer");
    object o = Activator.CreateInstance(TypeToReflect);
    MethodInfo mi = TypeToReflect.GetMethod("AssemblyWithIdentifer");
    object newObject = mi.Invoke(o,null);
    //alternatively
    //ConstructorInfo ctor = TypeToReflect.GetConstructor(new Type[]{TypeToReflect.GetType()});
    //ctor.Invoke(newObject, null);

    return newObject;
}

我可以得到内部类的类型,

我可以调用构造函数并实例化一个类型的对象。但是,因为我没有任何访问的内部类型,我不能把它和操纵它从那里。

I can get the type of the internal class,
I can call the constructor and instantiate an object of the type. However, since I don’t have any access to the internal type I can’t cast it and manipulate it from there.

我知道我可以使用 Reflection.Emit 根据该类型创建一个新类, m去那个路线,那么我可能只是复制我试图访问到我的测试项目的项目的全部内容。这将是非常浪费和无意义,并要求我从其他项目投入的东西,并创造一个混乱,这绝对不是我想去的路线在这个时候。

I understand I can use Reflection.Emit to create a new class based on that type, but if I'm going that route then I might as well just copy the entire contents of the project I'm trying to access into my test project. This would be really wastefully and pointless and would require me to throw in stuff from other projects and creating a mess and it's absolutely not the route I want to go at this time.

我见过的例子访问单个方法和属性,但没有实例化整个类。我不是100%肯定是可能的,因为在操作的顺序反射发生之前访问修饰符被看。

I've seen examples accessing individual methods and properties but none that instantiate an entire class. I'm not 100% sure it's possible since in the order of operations reflection happens before access modifiers are looked at.

可以这样做吗?如果是,怎么办?

Can this be done, and if so, how?

对象用于测试目的和 [Assembly:InternalsVisibleTo()] 不工作,由于我目前正在解决的错误。有关原始问题,请参见此处

For clairification sake I wanted to use the instantiated object for testing purposes and [Assembly:InternalsVisibleTo("")] wasn't working due to bug which I'm currently working around. See here for original question.

推荐答案

由于你只知道执行时的类型,真的没有这样的概念,返回对象作为内部类型。想想你想要的方法签名看起来像...没有办法表达它。

Given that you only know the type at execution time, there's really no such concept as "returning the object as the internal type". Think about what you'd want the method signature to look like... there's no way you could express it.

如果调用代码知道它在强类型方法,你应该使代码通用:

If the calling code knows about it in a strongly typed way, you should make the code generic instead:

public T InitPrivateCoreObjects<T>()
{
    Type type = typeof(T);
    ...
    return (T) newObject;
}

...但如果调用代码

... but if the calling code doesn't know about it, that's not helpful to it.

如果你能解释为什么你认为你想要这个能力,我们可以尝试建议替代品。

If you could explain more about why you think you want this ability, we could try to suggest alteratives.

这篇关于实例化一个内部类并将其转换为给定类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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