从Another Dll添加对Dll的引用 [英] Add reference to the Dll from Another Dll

查看:95
本文介绍了从Another Dll添加对Dll的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好...

开发了C#应用程序,用于创建一些功能的dll,我一次创建两个dll,例如Dll A和Dll B.

这个Dll A是Dll B的引用。

那么如何从Dll B调用Dll A.

我使用下面的代码,但是我收到了错误信息。



代码是



Hi All...
Am developed C# application for creating dll for some functionality,am creating two dll's at a time,for example Dll A and Dll B.
This Dll A is reference of Dll B.
So how to call Dll A from Dll B.
Am using following code for this, but i got error message.

the Code is

public class Proxy : MarshalByRefObject
        {
            public Assembly GetAssembly(string assemblyPath)
            {
                try
                {
                    return Assembly.LoadFile(assemblyPath);
                }
                catch (Exception)
                {
                    return null;
                    // throw new InvalidOperationException(ex);
                }
            }
        } 







string dynamicClassPath = File Path;


            AppDomain domains = AppDomain.CreateDomain("New domain name");
            Type type = typeof(Proxy);
            Proxy myObject = (Proxy)domains.CreateInstanceFromAndUnwrap(dynamicClassPath, type .FullName);





,错误是



and the error is

Quote:

无法从程序集'Dynamic_id,Version = 0.0.0.0加载'FolderName.FileName + Proxy'类型, Culture = neutral,PublicKeyToken = null'。

Could not load type 'FolderName.FileName+Proxy' from assembly 'Dynamic_id, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.



这里


here

Dynamic_id

是一个dll。





所以请帮我怎么做?

抱歉我的英语不好。







感谢您的回复Mr.Sergey

但您的回复对我没有帮助,现在我非常清楚地发布我的问题,

Dll A和B都是动态创建的。

以下代码在Dll A



is a dll.


so pls help me how to do this??
sorry my poor English.



Thanks for your reply Mr.Sergey
But your reply is not helpful for me, now I post my question very clearly,
Dll A and B both are created dynamically.
The Following code in Dll A

using System;

namespace ForTest
{
	public class Dynamic_Val_001
	{
		private string _Prop_Name;

		private string _Prop_Address;

		public virtual string Prop_Name
		{
			get
			{
				return this._Prop_Name;
			}
			set
			{
				this._Prop_Name = value;
			}
		}

		public virtual string Prop_Address
		{
			get
			{
				return this._Prop_Address;
			}
			set
			{
				this._Prop_Address = value;
			}
		}
	}
} 





Dll B代码是





the Dll B code is

using System;

namespace ForTest
{
	public class Val_001
	{
		public virtual Tuple<bool, string> isValid(Dynamic_Val_001)
		{
			return new Tuple<bool, string>(false, "Success");
		}
	}
}





现在Dll B有一个元组bool函数,其中一个参数,在那个参数是Dll A类名,所以Dll B取决于dll A.



那么如何从Dll B调用那个Dll A类。



Now the Dll B have a one tuple bool function with one parameter, In that parameter is Dll A class name,so Dll B is depends on dll A.

So how to Call that Dll A class from Dll B.

推荐答案

我没有检查你的所有代码示例;使用应用程序域时有许多微妙的时刻;也许你稍后会问不同的问题。



我只回答你的问题。错误是你 Assembly.LoadFile 方法。你必须使用 Assembly.LoadFrom

Assembly.LoadFrom Method(System.Reflection) [ ^ ]。

< br $> b $ b责备不是你自己,而是微软的命名和文档;关于这个话题,目前尚不清楚。



路径应该是正确的。如果这是一个简单的名称,它可能会产生问题。第一优先级是从当前工作目录加载,但这个目录可以是任何东西;这取决于用户如何启动应用程序。所以你最好使用可执行路径或相对于它的路径。请查看我过去的答案:

如何找到我的程序目录可执行目录),

如何找到我的程序目录当前目录,特殊文件夹



请记住这个路径问题。



-SA
I did not check up all your code sample; there is a number of delicate moments in working with application domains; perhaps you will ask separate questions later.

I'll only answer your question. The bug is you Assembly.LoadFile method. You have to use Assembly.LoadFrom:
Assembly.LoadFrom Method (System.Reflection)[^].

Blame not yourself but Microsoft naming and documentation; on this topic, it's not clear.

And the path should be correct. If this is a simple name, it can create a problem. First priority would be loading from a current working directory, but this directory could be anything; it depends on how the user starts the application. So you better should use, say, executable path or a path relative to it. Please see my past answers:
How to find my programs directory (executable directory),
How to find my programs directory (current directory, "special folders").

Just keep this path issue in mind.

—SA


这篇关于从Another Dll添加对Dll的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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