简体   繁体   English

在没有任何代码的情况下访问Web服务中的内部类和方法

[英]Accessing internal classes and methods in a webservice with no code behind

I have a webservice in a certain namespace, let's call it MyNamespace . 我在某个命名空间中有一个Web服务,我们称它为MyNamespace It's a normal webservice with webmethods in the code behind. 这是一个普通的Web服务,后面的代码中包含Web方法。 I also have an additional .cs file with all kinds of classes used in those webmethods. 我还有一个附加的.cs文件,其中包含这些Web方法中使用的各种类。 So far so good, right? 到目前为止一切顺利,对吗?

Then I have an additional webservice used for testing stuff only. 然后,我还有一个仅用于测试内容的Web服务。 It has no code behind and its code is directly in the .asmx file so I can quickly upload it to a server without rebuilding anything. 它没有任何代码,其代码直接位于.asmx文件中,因此我可以快速将其上传到服务器,而无需重建任何内容。 This test webservice is in the same MyNamespace namespace as the webservice mentioned in the first paragraph and in the same project as well. 该测试Web服务与MyNamespace提到的Web服务位于同一MyNamespace命名空间中,并且也位于同一项目中。 It works great and all until I try to access internal members of MyNamespace . 在我尝试访问MyNamespace内部成员之前,它MyNamespace It cannot be done, they are invisible, as if they were private. 它们是看不见的,就好像它们是私人的一样,这无法完成。

This is the point where I might start to write nonsense, so please correct me if necessary. 这就是我可能开始胡说八道的地方,因此,如有必要,请纠正我。 I have looked around a bit and it seems this is 'intended' because an .asmx without a proper code behind is compiled at runtime and is not a part of a proper assembly. 我四处张望,似乎这是“预期的”,因为在运行时会编译没有适当代码的.asmx,而不是适当程序集的一部分。 Since internal members are available only within the assembly, it's entirely correct for them to be not available in my .asmx code. 由于内部成员仅在程序集中可用,因此在我的.asmx代码中不提供内部成员是完全正确的。 My question then is - can I access these members somehow without moving my code to a .asmx.cs code behind file? 然后我的问题是-我可以以某种方式访问​​这些成员,而无需将代码移到文件后面的.asmx.cs代码吗?

I would imagine this is possible using reflection. 我想通过反射可以做到这一点。

For example: To invoke an internal method on a public class: 例如:在公共类上调用内部方法:

var myPublicClass = new PublicClass;
MethodInfo mInfo = typeof(PublicClass).GetMethod("InternalMethodName", BindingFlags.Instance | BindingFlags.NonPublic);

var result = mInfo.Invoke(myPublicCLass, null) // (replace null with an array of parameters if necessary)

Or to create in instance of a internal class: 或在内部类的实例中创建:

MethodInfo mInfo= Type.GetType(assemblyQualifiedName).GetConstructor(
                        BindingFlags.NonPublic | BindingFlags.Instance,
                        null,
                        new[] { typeof(string) },
                        null
                    );
object myInstance = mInfo.Invoke(new[] { "constructorArgument" });

Edit: Internal static method on internal static class: 编辑:内部静态类上的内部静态方法:

MethodInfo mInfo = Type.GetType(assemblyQualifiedName).GetMethod("InternalMethodName", BindingFlags.Static| BindingFlags.NonPublic);

var result = mInfo.Invoke(null, null) // (replace 2nd null with an array of parameters if necessary)

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

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