Skip to content

Commit 29bddaa

Browse files
committed
resets
1 parent 3007def commit 29bddaa

File tree

7 files changed

+380
-0
lines changed

7 files changed

+380
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
3+
// System.Runtime.Remoting.RemotingConfiguration.Configure
4+
// System.Runtime.Remoting.RemotingConfiguration.GetRegisteredWellKnownServiceTypes
5+
/*
6+
The following example demonstrates the 'Configure' and
7+
'GetRegisteredWellKnownServiceTypes' methods of 'RemotingConfiguration' class.
8+
It configures the remoting infrastructure using the 'Configure' method.Then gets
9+
the registered well-known objects at the service end and displays it's properties
10+
on the console.
11+
*/
12+
#using <System.Runtime.Remoting.dll>
13+
#using <System.dll>
14+
15+
using namespace System;
16+
using namespace System::Runtime::Remoting;
17+
using namespace System::Runtime::Remoting::Channels;
18+
using namespace System::Runtime::Remoting::Channels::Tcp;
19+
using namespace System::Runtime::Remoting::Channels::Http;
20+
21+
int main()
22+
{
23+
// <Snippet1>
24+
// Configure the remoting structure.
25+
RemotingConfiguration::Configure( "server.config" );
26+
// </Snippet1>
27+
28+
// <Snippet2>
29+
// Retrive the array of objects registered as well known types at
30+
// the service end.
31+
array<WellKnownServiceTypeEntry^>^myEntries = RemotingConfiguration::GetRegisteredWellKnownServiceTypes();
32+
Console::WriteLine( "The number of WellKnownServiceTypeEntries are:{0}", myEntries->Length );
33+
Console::WriteLine( "The Object Type is:{0}", myEntries[ 0 ]->ObjectType );
34+
Console::WriteLine( "The Object Uri is:{0}", myEntries[ 0 ]->ObjectUri );
35+
Console::WriteLine( "The Mode is:{0}", myEntries[ 0 ]->Mode );
36+
// </Snippet2>
37+
38+
Console::WriteLine( "Press <enter> to exit..." );
39+
Console::ReadLine();
40+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// System.Reflection.Emit.FieldBuilder.SetOffset
2+
// System.Reflection.Emit.FieldBuilder.SetMarshal
3+
4+
/*
5+
The following program demonstrates 'SetOffset' and 'SetMarshal'
6+
methods of 'FieldBuilder' class.A new Class is defined and a
7+
'PInvoke' method 'OpenFile' method of 'Kernel32.dll' is defined
8+
in the class.Instance of the class is created and the method is invoked.
9+
To execute this program, make sure a file named 'MyFile.txt' should be there
10+
in the current directory.
11+
*/
12+
13+
// <Snippet1>
14+
using System;
15+
using System.Runtime.InteropServices;
16+
using System.Threading;
17+
using System.Reflection;
18+
using System.Reflection.Emit;
19+
using System.Security.Permissions;
20+
using System.Runtime.CompilerServices;
21+
22+
public class FieldBuilder_Sample
23+
{
24+
public static Type CreateType(AppDomain currentDomain)
25+
{
26+
// Create an assembly.
27+
AssemblyName myAssemblyName = new AssemblyName();
28+
myAssemblyName.Name = "DynamicAssembly";
29+
AssemblyBuilder myAssembly =
30+
currentDomain.DefineDynamicAssembly(myAssemblyName,AssemblyBuilderAccess.RunAndSave);
31+
// Create a dynamic module in Dynamic Assembly.
32+
ModuleBuilder myModuleBuilder=myAssembly.DefineDynamicModule("MyModule","MyModule.mod");
33+
// Define a public class named "MyClass" in the assembly.
34+
TypeBuilder myTypeBuilder= myModuleBuilder.DefineType("MyClass",TypeAttributes.Public);
35+
TypeBuilder myTypeBuilder2 = myModuleBuilder.DefineType("MyClass2",
36+
TypeAttributes.Public|TypeAttributes.BeforeFieldInit|TypeAttributes.SequentialLayout|TypeAttributes.AnsiClass|TypeAttributes.Sealed);
37+
FieldBuilder myFieldBuilder1= myTypeBuilder2.DefineField("myBytes1",
38+
typeof(byte),FieldAttributes.Public);
39+
FieldBuilder myFieldBuilder2= myTypeBuilder2.DefineField("myBytes2",
40+
typeof(byte),FieldAttributes.Public);
41+
FieldBuilder myFieldBuilder3= myTypeBuilder2.DefineField("myErrorCode",
42+
typeof(short),FieldAttributes.Public);
43+
FieldBuilder myFieldBuilder4= myTypeBuilder2.DefineField("myReserved1",
44+
typeof(short),FieldAttributes.Public);
45+
FieldBuilder myFieldBuilder5= myTypeBuilder2.DefineField("myReserved2",
46+
typeof(short),FieldAttributes.Public);
47+
FieldBuilder myFieldBuilder6= myTypeBuilder2.DefineField("myPathName",
48+
typeof(char[]),FieldAttributes.Public);
49+
myFieldBuilder6.SetMarshal(UnmanagedMarshal.DefineByValArray(128));
50+
myFieldBuilder6.SetOffset(4);
51+
Type myType1 = myTypeBuilder2.CreateType();
52+
// Create the PInvoke method for 'OpenFile' method of 'Kernel32.dll'.
53+
Type[] myParameters={ typeof(string), myType1 ,typeof(uint)};
54+
MethodBuilder myMethodBuilder= myTypeBuilder.DefinePInvokeMethod("OpenFile",
55+
"kernel32.dll",MethodAttributes.Public|MethodAttributes.Static|MethodAttributes.HideBySig,
56+
CallingConventions.Standard,typeof(IntPtr),
57+
myParameters,CallingConvention.Winapi,CharSet.None);
58+
Type myAttributeType = typeof(MethodImplAttribute);
59+
ConstructorInfo myConstructorInfo =
60+
myAttributeType.GetConstructor(new Type[1]{typeof(MethodImplOptions)});
61+
CustomAttributeBuilder myAttributeBuilder = new CustomAttributeBuilder(myConstructorInfo,
62+
new object[1]{MethodImplOptions.PreserveSig});
63+
myMethodBuilder.SetCustomAttribute(myAttributeBuilder);
64+
ParameterBuilder myParameterBuilder2=myMethodBuilder.DefineParameter(2,
65+
ParameterAttributes.Out,"myClass2");
66+
Type myType=myTypeBuilder.CreateType();
67+
myAssembly.Save("EmittedAssembly.dll");
68+
return myType;
69+
}
70+
71+
public static void Main()
72+
{
73+
try
74+
{
75+
Type myType = CreateType(Thread.GetDomain());
76+
Type myClass2 = myType.Module.GetType("MyClass2");
77+
object myParam2 = Activator.CreateInstance(myClass2);
78+
uint myUint=0x00000800;
79+
object[] myArgs= {"MyFile.Txt",myParam2,myUint};
80+
Object myObject = myType.InvokeMember("OpenFile",BindingFlags.Public |
81+
BindingFlags.InvokeMethod | BindingFlags.Static,null,null,myArgs);
82+
Console.WriteLine("MyClass.OpenFile method returned: \"{0}\"", myObject);
83+
}
84+
catch(Exception e)
85+
{
86+
Console.WriteLine("Exception Caught "+e.Message);
87+
}
88+
}
89+
}
90+
// </Snippet1>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// System.Runtime.Remoting.RemotingConfiguration.Configure
2+
// System.Runtime.Remoting.RemotingConfiguration.GetRegisteredWellKnownServiceTypes
3+
4+
/*
5+
The following example demonstrates the 'Configure' and
6+
'GetRegisteredWellKnownServiceTypes' methods of 'RemotingConfiguration' class.
7+
It configures the remoting infrastructure using the 'Configure' method.Then gets
8+
the registered well-known objects at the service end and displays it's properties
9+
on the console.
10+
*/
11+
12+
using System;
13+
using System.Runtime.Remoting;
14+
using System.Runtime.Remoting.Channels;
15+
using System.Runtime.Remoting.Channels.Tcp;
16+
using System.Runtime.Remoting.Channels.Http;
17+
18+
public class Sample
19+
{
20+
public static void Main()
21+
{
22+
// <Snippet1>
23+
24+
// Configure the remoting structure.
25+
RemotingConfiguration.Configure("server.config");
26+
27+
// </Snippet1>
28+
// <Snippet2>
29+
30+
// Retrive the array of objects registered as well known types at
31+
// the service end.
32+
WellKnownServiceTypeEntry[] myEntries =
33+
RemotingConfiguration.GetRegisteredWellKnownServiceTypes();
34+
Console.WriteLine("The number of WellKnownServiceTypeEntries are:"
35+
+myEntries.Length);
36+
Console.WriteLine("The Object Type is:"+myEntries[0].ObjectType);
37+
Console.WriteLine("The Object Uri is:"+myEntries[0].ObjectUri);
38+
Console.WriteLine("The Mode is:"+myEntries[0].Mode);
39+
40+
// </Snippet2>
41+
42+
Console.WriteLine("Press <enter> to exit...");
43+
Console.ReadLine();
44+
}
45+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// <Snippet1>
2+
using System;
3+
using System.Threading;
4+
5+
class ApartmentTest
6+
{
7+
static void Main()
8+
{
9+
Thread newThread =
10+
new Thread(new ThreadStart(ThreadMethod));
11+
newThread.SetApartmentState(ApartmentState.MTA);
12+
13+
Console.WriteLine("ThreadState: {0}, ApartmentState: {1}",
14+
newThread.ThreadState, newThread.GetApartmentState());
15+
16+
newThread.Start();
17+
18+
// Wait for newThread to start and go to sleep.
19+
Thread.Sleep(300);
20+
try
21+
{
22+
// This causes an exception since newThread is sleeping.
23+
newThread.SetApartmentState(ApartmentState.STA);
24+
}
25+
catch(ThreadStateException stateException)
26+
{
27+
Console.WriteLine("\n{0} caught:\n" +
28+
"Thread is not in the Unstarted or Running state.",
29+
stateException.GetType().Name);
30+
Console.WriteLine("ThreadState: {0}, ApartmentState: {1}",
31+
newThread.ThreadState, newThread.GetApartmentState());
32+
}
33+
}
34+
35+
static void ThreadMethod()
36+
{
37+
Thread.Sleep(1000);
38+
}
39+
}
40+
// </Snippet1>
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
' System.Reflection.Emit.FieldBuilder.SetOffset
2+
' System.Reflection.Emit.FieldBuilder.SetMarshal
3+
4+
' The following program demonstrates 'SetOffset' and 'SetMarshal'
5+
' methods of 'FieldBuilder' class.A new Class is defined and a
6+
' 'PInvoke' method 'OpenFile' method of 'Kernel32.dll' is defined
7+
' in the class.Instance of the class is created and the method is invoked.
8+
' To execute this program, make sure a file named'MyFile.txt' should be there
9+
' in the current directory.
10+
11+
' <Snippet1>
12+
Imports System.Runtime.InteropServices
13+
Imports System.Threading
14+
Imports System.Reflection
15+
Imports System.Reflection.Emit
16+
Imports System.Security.Permissions
17+
Imports System.Runtime.CompilerServices
18+
19+
Public Class FieldBuilder_Sample
20+
21+
Public Shared Function CreateType(ByVal currentDomain As AppDomain) As Type
22+
23+
' Create an assembly.
24+
Dim myAssemblyName As New AssemblyName()
25+
myAssemblyName.Name = "DynamicAssembly"
26+
Dim myAssembly As AssemblyBuilder = currentDomain.DefineDynamicAssembly(myAssemblyName, _
27+
AssemblyBuilderAccess.RunAndSave)
28+
' Create a dynamic module in Dynamic Assembly.
29+
Dim myModuleBuilder As ModuleBuilder = myAssembly.DefineDynamicModule("MyModule", _
30+
"MyModule.mod")
31+
' Define a public class named "MyClass" in the assembly.
32+
Dim myTypeBuilder As TypeBuilder = myModuleBuilder.DefineType("MyClass", _
33+
TypeAttributes.Public)
34+
Dim myTypeBuilder2 As TypeBuilder = myModuleBuilder.DefineType("MyClass2", _
35+
TypeAttributes.Public Or TypeAttributes.BeforeFieldInit Or _
36+
TypeAttributes.SequentialLayout Or TypeAttributes.AnsiClass Or TypeAttributes.Sealed)
37+
Dim myFieldBuilder1 As FieldBuilder = myTypeBuilder2.DefineField("myBytes1", _
38+
GetType(Byte), FieldAttributes.Public)
39+
Dim myFieldBuilder2 As FieldBuilder = myTypeBuilder2.DefineField("myBytes2", _
40+
GetType(Byte), FieldAttributes.Public)
41+
Dim myFieldBuilder3 As FieldBuilder = myTypeBuilder2.DefineField("myErrorCode", _
42+
GetType(Short), FieldAttributes.Public)
43+
Dim myFieldBuilder4 As FieldBuilder = myTypeBuilder2.DefineField("myReserved1", _
44+
GetType(Short), FieldAttributes.Public)
45+
Dim myFieldBuilder5 As FieldBuilder = myTypeBuilder2.DefineField("myReserved2", _
46+
GetType(Short), FieldAttributes.Public)
47+
Dim myFieldBuilder6 As FieldBuilder = myTypeBuilder2.DefineField("myPathName", _
48+
GetType(Char()), FieldAttributes.Public)
49+
myFieldBuilder6.SetMarshal(UnmanagedMarshal.DefineByValArray(128))
50+
myFieldBuilder6.SetOffset(4)
51+
Dim myType1 As Type = myTypeBuilder2.CreateType()
52+
' Create the PInvoke method for 'OpenFile' method of 'Kernel32.dll'.
53+
Dim myParameters As Type() = {GetType(String), myType1, GetType(System.UInt32)}
54+
Dim myMethodBuilder As MethodBuilder = myTypeBuilder.DefinePInvokeMethod("OpenFile", _
55+
"kernel32.dll", MethodAttributes.Public Or MethodAttributes.Static Or _
56+
MethodAttributes.HideBySig, CallingConventions.Standard, GetType(IntPtr), _
57+
myParameters, CallingConvention.Winapi, CharSet.None)
58+
Dim myAttributeType As Type = GetType(MethodImplAttribute)
59+
Dim myConstructorInfo As ConstructorInfo = myAttributeType.GetConstructor(New Type(0) _
60+
{GetType(MethodImplOptions)})
61+
Dim myAttributeBuilder As New CustomAttributeBuilder(myConstructorInfo, _
62+
New Object() {MethodImplOptions.PreserveSig})
63+
myMethodBuilder.SetCustomAttribute(myAttributeBuilder)
64+
Dim myParameterBuilder2 As ParameterBuilder = myMethodBuilder.DefineParameter(2, _
65+
ParameterAttributes.Out, "myClass2")
66+
Dim myType As Type = myTypeBuilder.CreateType()
67+
myAssembly.Save("EmittedAssembly.dll")
68+
Return myType
69+
End Function 'CreateType
70+
71+
<PermissionSetAttribute(SecurityAction.Demand, Name:="FullTrust")> _
72+
Public Shared Sub Main()
73+
Try
74+
Dim myType As Type = CreateType(Thread.GetDomain())
75+
Dim myClass2 As Type = myType.Module.GetType("MyClass2")
76+
Dim myParam2 As Object = Activator.CreateInstance(myClass2)
77+
Dim myUint As System.UInt32
78+
myUint.Parse("800")
79+
80+
Dim myArgs As Object() = {"MyFile.Txt", myParam2, myUint}
81+
Dim myObject As Object = myType.InvokeMember("OpenFile", _
82+
BindingFlags.Public Or BindingFlags.InvokeMethod Or _
83+
BindingFlags.Static, Nothing, Nothing, myArgs)
84+
Console.WriteLine("MyClass.OpenFile method returned: '{0}'", myObject)
85+
Catch e As Exception
86+
Console.WriteLine("Exception Caught: " & e.Message)
87+
End Try
88+
End Sub
89+
End Class
90+
' </Snippet1>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
' System.Runtime.Remoting.RemotingConfiguration.Configure
2+
' System.Runtime.Remoting.RemotingConfiguration.GetRegisteredWellKnownServiceTypes
3+
'
4+
' The following example demonstrates the 'Configure' and
5+
' 'GetRegisteredWellKnownServiceTypes' methods of 'RemotingConfiguration' class.
6+
' It configures the remoting infrastructure using the 'Configure' method.Then gets
7+
' the registered well-known objects at the service end and displays it's properties
8+
' on the console.
9+
10+
Imports System.Runtime.Remoting
11+
Imports System.Runtime.Remoting.Channels
12+
Imports System.Runtime.Remoting.Channels.Tcp
13+
Imports System.Runtime.Remoting.Channels.Http
14+
15+
Public Class Sample
16+
17+
Public Shared Sub Main()
18+
' <Snippet1>
19+
' Configure the remoting structure.
20+
RemotingConfiguration.Configure("server.config")
21+
22+
' </Snippet1>
23+
' <Snippet2>
24+
25+
' Retrive the array of objects registered as well known types at
26+
' the service end.
27+
Dim myEntries As WellKnownServiceTypeEntry() = _
28+
RemotingConfiguration.GetRegisteredWellKnownServiceTypes()
29+
Console.WriteLine("The number of WellKnownServiceTypeEntries are:" + myEntries.Length.ToString())
30+
Console.WriteLine("The Object Type is:" + myEntries(0).ObjectType.ToString())
31+
Console.WriteLine("The Object Uri is:" + myEntries(0).ObjectUri)
32+
Console.WriteLine("The Mode is:" + myEntries(0).Mode.ToString())
33+
34+
' </Snippet2>
35+
Console.WriteLine("Press <enter> to exit...")
36+
Console.ReadLine()
37+
End Sub
38+
End Class
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
' <Snippet1>
2+
Imports System.Threading
3+
4+
Public Class ApartmentTest
5+
6+
<MTAThread> _
7+
Shared Sub Main()
8+
9+
Dim newThread As Thread = New Thread(AddressOf ThreadMethod)
10+
newThread.SetApartmentState(ApartmentState.MTA)
11+
12+
Console.WriteLine("ThreadState: {0}, ApartmentState: {1}", _
13+
newThread.ThreadState, newThread.GetApartmentState())
14+
15+
newThread.Start()
16+
17+
' Wait for newThread to start and go to sleep.
18+
Thread.Sleep(300)
19+
Try
20+
' This causes an exception since newThread is sleeping.
21+
newThread.SetApartmentState(ApartmentState.STA)
22+
Catch stateException As ThreadStateException
23+
Console.WriteLine(vbCrLf & "{0} caught:" & vbCrLf & _
24+
"Thread is not In the Unstarted or Running state.", _
25+
stateException.GetType().Name)
26+
Console.WriteLine("ThreadState: {0}, ApartmentState: " & _
27+
"{1}", newThread.ThreadState, newThread.GetApartmentState())
28+
End Try
29+
30+
End Sub
31+
32+
Shared Sub ThreadMethod()
33+
Thread.Sleep(1000)
34+
End Sub
35+
36+
End Class
37+
' </Snippet1>

0 commit comments

Comments
 (0)