从程序集中加载接口并使用其方法
我目前正在学习 .NET 和 C#,所以我对它还很陌生,我必须使用服务器和客户端创建一个“通讯录”。我创建了一个服务器使用的接口,它描述了此通讯录可用的操作,如下所示:
bool AjouterContact(string num, string nom, string prenom, string mail, string telephone);
bool SupprimerContact(string num);
bool ModifierContact(string num, string nom, string prenom, string mail, string telephone);
List<string[]> RecupererContacts();
我曾经在客户端中引用该接口的 .dll,它工作得很好,但我现在需要动态加载该 .dll 。这就是我正在做的:
Assembly a = Assembly.LoadFrom("../../../RemotingInterfaces/bin/Debug/RemotingInterfaces.dll");
Module[] modules = a.GetModules();
Module module = modules[0];
Type[] types = module.GetTypes();
foreach (Type type in types)
{
Console.WriteLine(" Le type {0} a cette (ces ) methode (s) : ", type.Name);
Console.WriteLine("Type information for:" + type.FullName);
Console.WriteLine("\tIs Class = " + type.IsClass);
Console.WriteLine("\tIs Enum = " + type.IsEnum);
Console.WriteLine("\tAttributes = " + type.Attributes);
MethodInfo[] mInfo = type.GetMethods();
foreach (MethodInfo mi in mInfo)
Console.WriteLine(" {0}", mi);
}
这可以工作并在控制台中写入所有方法。但我想知道如何使用这些方法。
我希望我说得足够清楚。再说一次,我是 .NET 和 C# 的新手,所以我真的不知道它是如何工作的。
I'm currently studying .NET and C# so I'm pretty new to it and I have to create a 'contact book' with a server and a client. I created an interface used by the server that describes the operations available for this contact book like below:
bool AjouterContact(string num, string nom, string prenom, string mail, string telephone);
bool SupprimerContact(string num);
bool ModifierContact(string num, string nom, string prenom, string mail, string telephone);
List<string[]> RecupererContacts();
I used to refer to the .dll of that interface in my client and it worked fine, but I now need to load this .dll all dynamically. This is what I'm doing :
Assembly a = Assembly.LoadFrom("../../../RemotingInterfaces/bin/Debug/RemotingInterfaces.dll");
Module[] modules = a.GetModules();
Module module = modules[0];
Type[] types = module.GetTypes();
foreach (Type type in types)
{
Console.WriteLine(" Le type {0} a cette (ces ) methode (s) : ", type.Name);
Console.WriteLine("Type information for:" + type.FullName);
Console.WriteLine("\tIs Class = " + type.IsClass);
Console.WriteLine("\tIs Enum = " + type.IsEnum);
Console.WriteLine("\tAttributes = " + type.Attributes);
MethodInfo[] mInfo = type.GetMethods();
foreach (MethodInfo mi in mInfo)
Console.WriteLine(" {0}", mi);
}
This works and writes all the methods in the console. But I would like to know how to use these methods.
I hope I was clear enough. Again, I'm new to .NET and C# so I don't really know how it works.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

接口只是一个契约,它是属性和方法的列表,而不是实际的实现。您正在使用的 dll 中的接口看起来像这样
此时,方法 GetTheNumberOfStarsInTheSky() 尚未实现,无法使用。
最重要的是,您可以获得接口,但无法使用它的方法,因为它尚未定义。
希望这有帮助。
The interface is just a contract, it's a list of properties and methods and not actual implementation. The interface looks something like this in the dll you are working with
At this point the method GetTheNumberOfStarsInTheSky() is not implemented yet and cannot be used.
Bottom line is, you can get the interface but you cannot use it's methods because it's not defined yet.
Hope this helps.