Skip to content

Commit 75470d5

Browse files
committed
resets
1 parent fd1d0eb commit 75470d5

File tree

47 files changed

+2594
-59
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+2594
-59
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
//<Snippet1>
2+
using namespace System;
3+
using namespace System::Reflection;
4+
using namespace System::Reflection::Emit;
5+
6+
public ref class Example : MarshalByRefObject
7+
{
8+
public:
9+
void Test()
10+
{
11+
Assembly^ dynAssem = Assembly::Load(
12+
"DynamicHelloWorld, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");
13+
14+
Type^ myType = dynAssem->GetType("HelloWorld");
15+
myType->InvokeMember("HelloFromAD", BindingFlags::Public |
16+
BindingFlags::Static | BindingFlags::InvokeMethod,
17+
Type::DefaultBinder, nullptr, nullptr);
18+
}
19+
};
20+
21+
22+
static void GenerateDynamicAssembly(String^ location)
23+
{
24+
// Define the dynamic assembly and the module. There is only one
25+
// module in this assembly. Note that the call to DefineDynamicAssembly
26+
// specifies the location where the assembly will be saved. The
27+
// assembly version is 1.0.0.0.
28+
//
29+
AssemblyName^ asmName = gcnew AssemblyName("DynamicHelloWorld");
30+
asmName->Version = gcnew Version("1.0.0.0");
31+
32+
AssemblyBuilder^ ab =
33+
AppDomain::CurrentDomain->DefineDynamicAssembly(
34+
asmName, AssemblyBuilderAccess::Save, location);
35+
36+
String^ moduleName = asmName->Name + ".exe";
37+
ModuleBuilder^ mb = ab->DefineDynamicModule(asmName->Name, moduleName);
38+
39+
// Define the "HelloWorld" type, with one static method.
40+
TypeBuilder^ tb = mb->DefineType("HelloWorld", TypeAttributes::Public);
41+
MethodBuilder^ hello = tb->DefineMethod("HelloFromAD",
42+
MethodAttributes::Public | MethodAttributes::Static, nullptr, nullptr);
43+
44+
// The method displays a message that contains the name of the application
45+
// domain where the method is executed.
46+
ILGenerator^ il = hello->GetILGenerator();
47+
il->Emit(OpCodes::Ldstr, "Hello from '{0}'!");
48+
il->Emit(OpCodes::Call, AppDomain::typeid->GetProperty("CurrentDomain")->GetGetMethod());
49+
il->Emit(OpCodes::Call, AppDomain::typeid->GetProperty("FriendlyName")->GetGetMethod());
50+
il->Emit(OpCodes::Call, Console::typeid->GetMethod("WriteLine",
51+
gcnew array<Type^> { String::typeid, String::typeid }));
52+
il->Emit(OpCodes::Ret);
53+
54+
// Complete the HelloWorld type and save the assembly. The assembly
55+
// is placed in the location specified by DefineDynamicAssembly.
56+
Type^ myType = tb->CreateType();
57+
ab->Save(moduleName);
58+
};
59+
60+
void main()
61+
{
62+
// Prepare to create a new application domain.
63+
AppDomainSetup^ setup = gcnew AppDomainSetup();
64+
65+
// Set the application name before setting the dynamic base.
66+
setup->ApplicationName = "Example";
67+
68+
// Set the location of the base directory where assembly resolution
69+
// probes for dynamic assemblies. Note that the hash code of the
70+
// application name is concatenated to the base directory name you
71+
// supply.
72+
setup->DynamicBase = "C:\\DynamicAssemblyDir";
73+
Console::WriteLine("DynamicBase is set to '{0}'.", setup->DynamicBase);
74+
75+
AppDomain^ ad = AppDomain::CreateDomain("MyDomain", nullptr, setup);
76+
77+
// The dynamic directory name is the dynamic base concatenated with
78+
// the application name: <DynamicBase>\<hash code>\<ApplicationName>
79+
String^ dynamicDir = ad->DynamicDirectory;
80+
Console::WriteLine("Dynamic directory is '{0}'.", dynamicDir);
81+
82+
// The AssemblyBuilder won't create this directory automatically.
83+
if (!System::IO::Directory::Exists(dynamicDir))
84+
{
85+
Console::WriteLine("Creating the dynamic directory.");
86+
System::IO::Directory::CreateDirectory(dynamicDir);
87+
}
88+
89+
// Generate a dynamic assembly and store it in the dynamic
90+
// directory.
91+
GenerateDynamicAssembly(dynamicDir);
92+
93+
// Create an instance of the Example class in the application domain,
94+
// and call its Test method to load the dynamic assembly and use it.
95+
Example^ ex = (Example^) ad->CreateInstanceAndUnwrap(
96+
Example::typeid->Assembly->FullName, "Example");
97+
ex->Test();
98+
}
99+
100+
/* This example produces output similar to the following:
101+
102+
DynamicBase is set to 'C:\DynamicAssemblyDir\5e4a7545'.
103+
Dynamic directory is 'C:\DynamicAssemblyDir\5e4a7545\Example'.
104+
Creating the dynamic directory.
105+
Hello from 'MyDomain'!
106+
*/
107+
//</Snippet1>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
// <Snippet1>
3+
using namespace System;
4+
using namespace System::Reflection;
5+
static void InstantiateINT32( bool ignoreCase )
6+
{
7+
try
8+
{
9+
AppDomain^ currentDomain = AppDomain::CurrentDomain;
10+
Object^ instance = currentDomain->CreateInstanceAndUnwrap(
11+
"mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
12+
"SYSTEM.INT32",
13+
ignoreCase,
14+
BindingFlags::Default,
15+
nullptr,
16+
nullptr,
17+
nullptr,
18+
nullptr,
19+
nullptr );
20+
Console::WriteLine( instance->GetType() );
21+
}
22+
catch ( TypeLoadException^ e )
23+
{
24+
Console::WriteLine( e->Message );
25+
}
26+
27+
}
28+
29+
int main()
30+
{
31+
InstantiateINT32( false ); // Failed!
32+
InstantiateINT32( true ); // OK!
33+
}
34+
35+
// </Snippet1>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
// <Snippet1>
3+
using namespace System;
4+
using namespace System::Reflection;
5+
using namespace System::Reflection::Emit;
6+
ref class Test
7+
{
8+
public:
9+
static void InstantiateMyDynamicType( AppDomain^ domain )
10+
{
11+
try
12+
{
13+
14+
// You must supply a valid fully qualified assembly name here.
15+
domain->CreateInstance( "Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType" );
16+
}
17+
catch ( Exception^ e )
18+
{
19+
Console::WriteLine( e->Message );
20+
}
21+
22+
}
23+
24+
static Assembly^ MyResolveEventHandler( Object^ sender, ResolveEventArgs^ args )
25+
{
26+
return DefineDynamicAssembly( dynamic_cast<AppDomain^>(sender) );
27+
}
28+
29+
static Assembly^ DefineDynamicAssembly( AppDomain^ domain )
30+
{
31+
32+
// Build a dynamic assembly using Reflection Emit API.
33+
AssemblyName^ assemblyName = gcnew AssemblyName;
34+
assemblyName->Name = "MyDynamicAssembly";
35+
AssemblyBuilder^ assemblyBuilder = domain->DefineDynamicAssembly( assemblyName, AssemblyBuilderAccess::Run );
36+
ModuleBuilder^ moduleBuilder = assemblyBuilder->DefineDynamicModule( "MyDynamicModule" );
37+
TypeBuilder^ typeBuilder = moduleBuilder->DefineType( "MyDynamicType", TypeAttributes::Public );
38+
ConstructorBuilder^ constructorBuilder = typeBuilder->DefineConstructor( MethodAttributes::Public, CallingConventions::Standard, nullptr );
39+
ILGenerator^ ilGenerator = constructorBuilder->GetILGenerator();
40+
ilGenerator->EmitWriteLine( "MyDynamicType instantiated!" );
41+
ilGenerator->Emit( OpCodes::Ret );
42+
typeBuilder->CreateType();
43+
return assemblyBuilder;
44+
}
45+
46+
};
47+
48+
int main()
49+
{
50+
AppDomain^ currentDomain = AppDomain::CurrentDomain;
51+
Test::InstantiateMyDynamicType( currentDomain ); // Failed!
52+
currentDomain->AssemblyResolve += gcnew ResolveEventHandler( Test::MyResolveEventHandler );
53+
Test::InstantiateMyDynamicType( currentDomain ); // OK!
54+
}
55+
56+
// </Snippet1>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
using namespace System;
3+
4+
// <Snippet1>
5+
int main()
6+
{
7+
AppDomain^ currentDomain = AppDomain::CurrentDomain;
8+
AppDomain^ otherDomain = AppDomain::CreateDomain( "otherDomain" );
9+
currentDomain->ExecuteAssembly( "MyExecutable.exe" );
10+
11+
// Prints S"MyExecutable running on [default]"
12+
otherDomain->ExecuteAssembly( "MyExecutable.exe" );
13+
14+
// Prints S"MyExecutable running on otherDomain"
15+
}
16+
17+
// </Snippet1>
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
2+
// <Snippet1>
3+
using namespace System;
4+
using namespace System::IO;
5+
using namespace System::Reflection;
6+
using namespace System::Reflection::Emit;
7+
void InstantiateMyType( AppDomain^ domain )
8+
{
9+
try
10+
{
11+
12+
// You must supply a valid fully qualified assembly name here.
13+
domain->CreateInstance( "Assembly text name, Version, Culture, PublicKeyToken", "MyType" );
14+
}
15+
catch ( Exception^ e )
16+
{
17+
Console::WriteLine( e->Message );
18+
}
19+
20+
}
21+
22+
23+
// Loads the content of a file to a Byte array.
24+
array<Byte>^ loadFile( String^ filename )
25+
{
26+
FileStream^ fs = gcnew FileStream( filename,FileMode::Open );
27+
array<Byte>^buffer = gcnew array<Byte>((int)fs->Length);
28+
fs->Read( buffer, 0, buffer->Length );
29+
fs->Close();
30+
return buffer;
31+
}
32+
33+
34+
// Creates a dynamic assembly with symbol information
35+
// and saves them to temp.dll and temp.pdb
36+
void EmitAssembly( AppDomain^ domain )
37+
{
38+
AssemblyName^ assemblyName = gcnew AssemblyName;
39+
assemblyName->Name = "MyAssembly";
40+
AssemblyBuilder^ assemblyBuilder = domain->DefineDynamicAssembly( assemblyName, AssemblyBuilderAccess::Save );
41+
ModuleBuilder^ moduleBuilder = assemblyBuilder->DefineDynamicModule( "MyModule", "temp.dll", true );
42+
TypeBuilder^ typeBuilder = moduleBuilder->DefineType( "MyType", TypeAttributes::Public );
43+
ConstructorBuilder^ constructorBuilder = typeBuilder->DefineConstructor( MethodAttributes::Public, CallingConventions::Standard, nullptr );
44+
ILGenerator^ ilGenerator = constructorBuilder->GetILGenerator();
45+
ilGenerator->EmitWriteLine( "MyType instantiated!" );
46+
ilGenerator->Emit( OpCodes::Ret );
47+
typeBuilder->CreateType();
48+
assemblyBuilder->Save( "temp.dll" );
49+
}
50+
51+
ref class Resolver
52+
{
53+
public:
54+
static Assembly^ MyResolver( Object^ sender, ResolveEventArgs^ args )
55+
{
56+
AppDomain^ domain = dynamic_cast<AppDomain^>(sender);
57+
58+
// Once the files are generated, this call is
59+
// actually no longer necessary.
60+
EmitAssembly( domain );
61+
array<Byte>^rawAssembly = loadFile( "temp.dll" );
62+
array<Byte>^rawSymbolStore = loadFile( "temp.pdb" );
63+
Assembly^ assembly = domain->Load( rawAssembly, rawSymbolStore );
64+
return assembly;
65+
}
66+
67+
};
68+
69+
int main()
70+
{
71+
AppDomain^ currentDomain = AppDomain::CurrentDomain;
72+
InstantiateMyType( currentDomain ); // Failed!
73+
currentDomain->AssemblyResolve += gcnew ResolveEventHandler( Resolver::MyResolver );
74+
InstantiateMyType( currentDomain ); // OK!
75+
}
76+
77+
// </Snippet1>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2+
3+
// <Snippet1>
4+
#using <system.dll>
5+
#using <system.messaging.dll>
6+
7+
using namespace System;
8+
using namespace System::Messaging;
9+
ref class MyNewQueue
10+
{
11+
public:
12+
void CountLowestPriority()
13+
{
14+
15+
// Holds the count of Lowest priority messages.
16+
UInt32 numberItems = 0;
17+
18+
// Connect to a queue.
19+
MessageQueue^ myQueue = gcnew MessageQueue( ".\\myQueue" );
20+
21+
// Get a cursor into the messages in the queue.
22+
MessageEnumerator^ myEnumerator = myQueue->GetMessageEnumerator();
23+
24+
// Specify that the messages's priority should be read.
25+
myQueue->MessageReadPropertyFilter->Priority = true;
26+
27+
// Move to the next message and examine its priority.
28+
while ( myEnumerator->MoveNext() )
29+
{
30+
31+
// Increase the count if priority is Lowest.
32+
if ( myEnumerator->Current->Priority == MessagePriority::Lowest )
33+
numberItems++;
34+
}
35+
36+
37+
// Display final count.
38+
Console::WriteLine( "Lowest priority messages: {0}", numberItems );
39+
return;
40+
}
41+
42+
};
43+
44+
int main()
45+
{
46+
47+
// Create a new instance of the class.
48+
MyNewQueue^ myNewQueue = gcnew MyNewQueue;
49+
50+
// Output the count of Lowest priority messages.
51+
myNewQueue->CountLowestPriority();
52+
return 0;
53+
}
54+
55+
// </Snippet1>

0 commit comments

Comments
 (0)