如何将对象类传递给DLL? [英] How do I pass a object class to a DLL?

查看:93
本文介绍了如何将对象类传递给DLL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行Windows 7 Enterprise 64位和Visual Studio 2012.程序和DLL库以及类都是.NET 4.0。



当它开始时该程序使用Dictionaries来包含在特定目录中找到的文件。如词典myDictionary< string,>其中字符串是文件名,int值是状态。

我试图将它传递给DLL函数。在DLL方面似乎没有问题,但在程序方面,它说我不能将字典用作类型定义。

我能想到的唯一解决方案是将字典封装在公共类中,然后将该类作为对象发送给函数。



所以我的过程是:



1.实例化新课程。示例:文件myFiles = new Files();

2.将我跟踪的所有文件添加到myFiles类中的Dictionaries并设置每个文件的默认状态。

3.在项目中引用DLL,然后使用using语句进行设置。它也在主项目中实例化。示例:ValidateFiles myvalidation = new ValidateFiles();

4.在DLL中有一个名为public void ValidateFile(object MyFiles){...}的函数,它将读取每一行并验证它是正确格式化。



我的问题是如何让Object MyFiles看到我创建的类中的Public方法和变量并作为参数发送? div class =h2_lin>解决方案

这里有一个基本问题,就是App需要引用DLL才能使用ValidateFiles类,而DLL需要引用应用程序,以便使用Files对象访问您尝试传递的Dictionary。



这是一个问题:它创建了一个循环依赖,这意味着如果这两个项目是同一个解决方案的一部分,你就无法编译!



将transport类移动到DLL中,以便DLL不需要引用App(从代码重用的角度来看这是一个糟糕的想法)或者创建一个第三个DLL来保存传输类和引用来自App和DLL。



BTW:直接传递一个字典是没有问题的,前提是这两个密钥和类类型由两个程序集(或两者引用的程序集)都知道


I am running Windows 7 Enterprise 64bit and Visual Studio 2012. The Program and the DLL Library and the class are all .NET 4.0.

As it started out the program had Dictionaries to contain the files found in a specific directory. Such as Dictionary myDictionary<string,> where the string was the filename and the int value was the status.
I tried to pass this along to the DLL function. On the DLL side there didn't seem to be a problem but on the program side it said that I could not use the Dictionary as a Type definition.
The only solution I could come up with was to encapsulate the dictionaries within a public class and then send that class to the function as an object.

So my Process is:

1.Instantiate the new class. Example: Files myFiles = new Files();
2.Add all the files I am tracking to the Dictionaries within the myFiles class and set the default status of each file.
3.The DLL is Referenced within the project and then set with a "using" statement. It is also instantiated within the main project. Example: ValidateFiles myvalidation = new ValidateFiles();
4.In the DLL there is a function called public void ValidateFile(object MyFiles) {...} which will read every line and verify that it is correctly formated.

The question I have is how do I get the Object MyFiles to see the Public methods and variables within the class I created and sent as an argument?

解决方案

There is a fundamental problem here, which is that the App needs a reference to the DLL in order to use the ValidateFiles class, and the DLL needs a reference to the App in order to use the Files object to access the Dictionary you are trying to pass.

This is a problem: it creates a circular dependency which means that if the two projects are part of the same solution, you can't compile!

Either move the "transport" class into the DLL so that the DLL doesn't need to reference the App (which is a poor idea from a code reuse perspective anyway) or create a third DLL to hold the transport" class and reference that from both the App and the DLL.

BTW: Passing a Dictionary directly is no problem, provided that both the Key and Value class types are known by both assemblies (or in an assembly referenced by both)


这篇关于如何将对象类传递给DLL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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