我目前正在学习 .NET 和 C#,所以我对它还很陌生,我必须创建一个包含服务器和客户端的“联系簿”。我创建了一个服务器使用的接口(interface),它描述了可用于此通讯簿的操作,如下所示:
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();
我曾经在我的客户端中引用该接口(interface)的 .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# 的新手,所以我真的不知道它是如何工作的。
请您参考如下方法:
接口(interface)只是一个契约,它是属性和方法的列表,而不是实际的实现。在您正在使用的 dll 中,界面看起来像这样
public interface IJustAListOfThingsToImplement
{
int GetTheNumberOfStarsInTheSky();
}
此时 GetTheNumberOfStarsInTheSky() 方法尚未实现,无法使用。
底线是,您可以获取接口(interface),但不能使用它的方法,因为它尚未定义。
希望这对您有所帮助。
