为程序集
程序集提供使运行库能够充分了解应用程序的内容并强制使用应用程序定义的版本控制和依赖项规则的结构。这些概念对解决版本控制问题和简化运行库应用程序的部署至关重要。using System;
using System.Reflection;
public class LoadInvoke
{
public static void Main(string[] args)
{
Assembly a = Assembly.LoadFrom(args[0]);
Type[] mytypes = a.GetTypes();
BindingFlags flags = (BindingFlags.NonPublic | BindingFlags.Public |
BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly); foreach(Type t in mytypes)
{
MethodInfo[] mi = t.GetMethods(flags);
Object obj = Activator.CreateInstance(t); foreach(MethodInfo m in mi)
{
m.Invoke(obj, null);
}
}
}
}[C++]
// LoadInvoke loads MyAssembly.dll and invokes the MyMethod1 method.
// After compiling this class, run LoadInvoke.exe with MyAssembly.dll
// as the command line argument, as shown below:
// LoadInvoke Myassembly.dll#using <mscorlib.dll>using namespace System;
using namespace System::Reflection;int main() {
String* args[] = Environment::GetCommandLineArgs();
Assembly* a = Assembly::LoadFrom(args[0]);
Type* mytypes[] = a->GetTypes();
BindingFlags flags = static_cast<BindingFlags>(BindingFlags::NonPublic | BindingFlags::Public |
BindingFlags::Static | BindingFlags::Instance | BindingFlags::DeclaredOnly); System::Collections::IEnumerator* myEnum = mytypes->GetEnumerator();
while (myEnum->MoveNext()) {
Type* t = __try_cast<Type*>(myEnum->Current); MethodInfo* mi[] = t->GetMethods(flags);
Object* obj = Activator::CreateInstance(t); System::Collections::IEnumerator* myEnum = mi->GetEnumerator();
while (myEnum->MoveNext()) {
MethodInfo* m = __try_cast<MethodInfo*>(myEnum->Current);
m->Invoke(obj, 0);
}
}
}[Visual Basic]
' Use this class with the LoadInvoke program.
' Compile this class using vbc /t:library MyAssembly.vb
' to obtain MyAssembly.dll.
Imports System
Imports Microsoft.VisualBasicPublic Class MyAssembly
Public Sub MyMethod1()
Console.WriteLine("Invoking MyAssembly.MyMethod1")
End Sub 'MyMethod1
End Class 'MyAssembly[C#]
// Use this class with the LoadInvoke program.
// Compile this class using csc /t:library MyAssembly.cs
// to obtain MyAssembly.dll.
using System;public class MyAssembly
{
public void MyMethod1()
{
Console.WriteLine("Invoking MyAssembly.MyMethod1");
}
}
程序集提供使运行库能够充分了解应用程序的内容并强制使用应用程序定义的版本控制和依赖项规则的结构。这些概念对解决版本控制问题和简化运行库应用程序的部署至关重要。using System;
using System.Reflection;
public class LoadInvoke
{
public static void Main(string[] args)
{
Assembly a = Assembly.LoadFrom(args[0]);
Type[] mytypes = a.GetTypes();
BindingFlags flags = (BindingFlags.NonPublic | BindingFlags.Public |
BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly); foreach(Type t in mytypes)
{
MethodInfo[] mi = t.GetMethods(flags);
Object obj = Activator.CreateInstance(t); foreach(MethodInfo m in mi)
{
m.Invoke(obj, null);
}
}
}
}[C++]
// LoadInvoke loads MyAssembly.dll and invokes the MyMethod1 method.
// After compiling this class, run LoadInvoke.exe with MyAssembly.dll
// as the command line argument, as shown below:
// LoadInvoke Myassembly.dll#using <mscorlib.dll>using namespace System;
using namespace System::Reflection;int main() {
String* args[] = Environment::GetCommandLineArgs();
Assembly* a = Assembly::LoadFrom(args[0]);
Type* mytypes[] = a->GetTypes();
BindingFlags flags = static_cast<BindingFlags>(BindingFlags::NonPublic | BindingFlags::Public |
BindingFlags::Static | BindingFlags::Instance | BindingFlags::DeclaredOnly); System::Collections::IEnumerator* myEnum = mytypes->GetEnumerator();
while (myEnum->MoveNext()) {
Type* t = __try_cast<Type*>(myEnum->Current); MethodInfo* mi[] = t->GetMethods(flags);
Object* obj = Activator::CreateInstance(t); System::Collections::IEnumerator* myEnum = mi->GetEnumerator();
while (myEnum->MoveNext()) {
MethodInfo* m = __try_cast<MethodInfo*>(myEnum->Current);
m->Invoke(obj, 0);
}
}
}[Visual Basic]
' Use this class with the LoadInvoke program.
' Compile this class using vbc /t:library MyAssembly.vb
' to obtain MyAssembly.dll.
Imports System
Imports Microsoft.VisualBasicPublic Class MyAssembly
Public Sub MyMethod1()
Console.WriteLine("Invoking MyAssembly.MyMethod1")
End Sub 'MyMethod1
End Class 'MyAssembly[C#]
// Use this class with the LoadInvoke program.
// Compile this class using csc /t:library MyAssembly.cs
// to obtain MyAssembly.dll.
using System;public class MyAssembly
{
public void MyMethod1()
{
Console.WriteLine("Invoking MyAssembly.MyMethod1");
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货