如何通过dll访问exe的成员? [英] How do I access the member of an exe through dll?

查看:45
本文介绍了如何通过dll访问exe的成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在同一解决方案下有两个项目。项目A的类型为Windows应用程序,项目B的类型为类库。

我需要通过项目B访问项目A的成员。

请让我知道如何我可以实现吗?

我不想将参数从一个项目传递到另一个项目。

I have two projects under same solution. Project A is of type Windows Application and Project B is of type Class Library.
I need to access the member of Project A via Project B.
Please let me know how can I achieve this?
and I don't want to pass the parameter from one project to another.

推荐答案

你真的应该从学习.NET和开发基础开始。首先,您需要了解项目的概念仅与开发和构建有关;在运行时,项目不涉及且不存在。即使您引用了另一个项目,在进行构建之后,引用的是生成的程序集,而不是项目。



您需要学习.NET,CLR和CLI的核心概念之一:程序集:

http://en.wikipedia.org/wiki/Assembly_%28CLI%29 [ ^ ],

http://en.wikipedia .org / wiki / Common_Language_Infrastructure [ ^ ],

http://en.wikipedia.org/wiki/Common_Language_Runtime [ ^ ],

http://msdn.microsoft.com/en-us/library/aa367757%28v=vs.85%29.aspx [ ^ ],

http:/ /msdn.microsoft.com/en-us/library/hk5f40ct%28v=vs.90%29.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/ms173099.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/ms973231.aspx [ ^ ]。



实际上,创建两个项目并将其放在同一个解决方案中是最好的。您可以通过其主可执行模块的名称(添加引用窗口中的浏览选项卡)进行引用和组装,但这不是最佳的维护。最好的方法是使用项目选项卡并引用另一个项目。这样,即使您移动/重命名一个或多个项目,签名,更改版本等,构建的程序集仍将保持引用。



当引用对象时,您可以访问所引用程序集的所有 public 成员及其所有 public 成员。还有另一种方法,在运行时,通过反射加载和使用程序集,甚至使用非公共类型和成员,但这个主题更先进。







你的最后一句话我不想将参数从一个项目传递给另一个项目根本没有意义。如果你仔细阅读我的答案,你会理解它,特别是如果你阅读了参考材料。







绝对无关紧要,哪些引用或引用程序集是DLL或EXE。程序集是程序集,那些PE文件只是程序集的模块。即使EXE通常没有被引用,因为它们是某些应用程序的主要可执行模块,但有一些技术很有用。



但是,重要的是不要创建它们循环引用。如果您只使用引用而不是反射,我无法理解您为什么要引用应用程序程序集。如果你解释一下,你可能会得到更改以获得进一步的建议。



-SA
You really should start with learning .NET and development basics. First of all, you need to understand that the concept of "project" is only related to development and build; during runtime, projects are not involved and do not exist. Even if you reference one project by another, after you do the build, the referenced are the resulting assemblies, not "projects".

You need to learn one of the central concepts of .NET, CLR and CLI: assembly:
http://en.wikipedia.org/wiki/Assembly_%28CLI%29[^],
http://en.wikipedia.org/wiki/Common_Language_Infrastructure[^],
http://en.wikipedia.org/wiki/Common_Language_Runtime[^],
http://msdn.microsoft.com/en-us/library/aa367757%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/hk5f40ct%28v=vs.90%29.aspx[^],
http://msdn.microsoft.com/en-us/library/ms173099.aspx[^],
http://msdn.microsoft.com/en-us/library/ms973231.aspx[^].

Practically, it's the best to create two projects and put the in the same solution. You can reference and assembly by the name of its main executable module ("Browse" tab in the "Add Reference" windows) but this is not the best for maintenance. The best way is to use "Projects" tab and reference one project by another. This way, the assemblies built will remain referenced even if you move/rename one or more projects, sign them, change versions, and so on.

When objects are reference, you can access all the public members of the referenced assemblies, and all their public members. There is another way, loading and using the assemblies during runtime, via reflection, and using even the non-public types and members, but this topic is way more advanced.



And your last statement "I don't want to pass the parameter from one project to another" simply makes no sense. You will understand it if you read my answer thoroughly, especially if you read the referenced material.



It is absolutely irrelevant, which of the referenced or referencing assemblies is "DLL" or "EXE". Assemblies are assemblies, and those PE files are just the modules of assemblies. Even though EXEs are usually not referenced because they are the main executable modules of some applications, there are some techniques where it is useful.

However, it's important not to create circular references. If you only use references and not reflection, I could not understand why would you want to reference the application assembly. If you explain that, you would probably get a change for further advice.

—SA


这篇关于如何通过dll访问exe的成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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