Skip to content

Commit dd55b06

Browse files
authored
AppDomain F# snippets (#7914)
1 parent d04ee76 commit dd55b06

File tree

73 files changed

+1925
-2
lines changed

Some content is hidden

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

73 files changed

+1925
-2
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// <Snippet1>
2+
open System
3+
4+
let printLoadedAssemblies (domain: AppDomain) =
5+
printfn "LOADED ASSEMBLIES:"
6+
for a in domain.GetAssemblies() do
7+
printfn $"{a.FullName}"
8+
printfn ""
9+
10+
let myAssemblyLoadEventHandler _ (args: AssemblyLoadEventArgs) =
11+
printfn $"ASSEMBLY LOADED: {args.LoadedAssembly.FullName}\n"
12+
13+
let currentDomain = AppDomain.CurrentDomain
14+
currentDomain.AssemblyLoad.AddHandler(AssemblyLoadEventHandler myAssemblyLoadEventHandler)
15+
16+
printLoadedAssemblies currentDomain
17+
// Lists mscorlib and this assembly
18+
19+
// You must supply a valid fully qualified assembly name here.
20+
currentDomain.CreateInstance("System.Windows.Forms, Version, Culture, PublicKeyToken", "System.Windows.Forms.TextBox")
21+
// Loads System, System.Drawing, System.Windows.Forms
22+
23+
printLoadedAssemblies currentDomain
24+
// Lists all five assemblies
25+
26+
27+
// </Snippet1>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="assemblyload.fs" />
9+
</ItemGroup>
10+
</Project>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
open System
2+
open System.Reflection
3+
4+
// <Snippet1>
5+
type MyType() =
6+
do
7+
printfn "\nMyType instantiated!"
8+
9+
let instantiateMyTypeFail (domain: AppDomain) =
10+
// Calling InstantiateMyType will always fail since the assembly info
11+
// given to CreateInstance is invalid.
12+
try
13+
// You must supply a valid fully qualified assembly name here.
14+
domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyType")
15+
|> ignore
16+
with e ->
17+
printfn $"\n{e.Message}"
18+
19+
let instantiateMyTypeSucceed (domain: AppDomain) =
20+
try
21+
let asmname = Assembly.GetCallingAssembly().FullName
22+
domain.CreateInstance(asmname, "MyType")
23+
|> ignore
24+
with e ->
25+
printfn $"\n{e.Message}"
26+
27+
let myResolveEventHandler _ _ =
28+
printfn "Resolving..."
29+
typeof<MyType>.Assembly
30+
31+
32+
let currentDomain = AppDomain.CurrentDomain
33+
34+
// This call will fail to create an instance of MyType since the
35+
// assembly resolver is not set
36+
instantiateMyTypeFail currentDomain
37+
38+
currentDomain.add_AssemblyResolve (ResolveEventHandler myResolveEventHandler)
39+
40+
// This call will succeed in creating an instance of MyType since the
41+
// assembly resolver is now set.
42+
instantiateMyTypeFail currentDomain
43+
44+
// This call will succeed in creating an instance of MyType since the
45+
// assembly name is valid.
46+
instantiateMyTypeSucceed currentDomain
47+
48+
// </Snippet1>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="assemblyresolve.fs" />
9+
</ItemGroup>
10+
</Project>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// <SNIPPET1>
2+
open System
3+
open System.IO
4+
5+
// Create application domain setup information
6+
let domaininfo = AppDomainSetup()
7+
domaininfo.ConfigurationFile <- Environment.CurrentDirectory + string Path.DirectorySeparatorChar + "ADSetup.exe.config"
8+
domaininfo.ApplicationBase <- Environment.CurrentDirectory
9+
10+
//Create evidence for the new appdomain from evidence of the current application domain
11+
let adEvidence = AppDomain.CurrentDomain.Evidence
12+
13+
// Create appdomain
14+
let domain = AppDomain.CreateDomain("Domain2", adEvidence, domaininfo)
15+
16+
// Display application domain information.
17+
printfn $"Host domain: {AppDomain.CurrentDomain.FriendlyName}"
18+
printfn $"Child domain: {domain.FriendlyName}\n"
19+
printfn $"Configuration file: {domain.SetupInformation.ConfigurationFile}"
20+
printfn $"Application Base Directory: {domain.BaseDirectory}"
21+
22+
AppDomain.Unload domain
23+
// The example displays output like the following:
24+
// Host domain: adsetup.exe
25+
// Child domain: Domain2
26+
//
27+
// Configuration file: C:\Test\ADSetup.exe.config
28+
// Application Base Directory: C:\Test
29+
// </SNIPPET1>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net48</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="adsetup.fs" />
9+
</ItemGroup>
10+
</Project>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// <SNIPPET1>
2+
open System
3+
4+
//Create evidence for new appdomain.
5+
let adevidence = AppDomain.CurrentDomain.Evidence
6+
7+
//Create the new application domain.
8+
let domain = AppDomain.CreateDomain("MyDomain", adevidence)
9+
10+
//Display the current relative search path.
11+
printfn $"Relative search path is: {domain.RelativeSearchPath}"
12+
13+
//Append the relative path.
14+
let Newpath = "www.code.microsoft.com"
15+
domain.AppendPrivatePath Newpath
16+
17+
//Display the new relative search path.
18+
printfn $"Relative search path is: {domain.RelativeSearchPath}"
19+
20+
//Clear the private search path.
21+
domain.ClearPrivatePath()
22+
23+
//Display the new relative search path.
24+
printfn $"Relative search path is now: {domain.RelativeSearchPath}"
25+
26+
AppDomain.Unload domain
27+
// </SNIPPET1>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net48</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="adclearprivatepath.fs" />
9+
</ItemGroup>
10+
</Project>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// <Snippet1>
2+
open System
3+
open System.Runtime.InteropServices
4+
5+
[<ComVisible true>]
6+
type MyComVisibleType() =
7+
do
8+
printfn "MyComVisibleType instantiated!"
9+
10+
[<ComVisible false>]
11+
type MyComNonVisibleType() =
12+
do
13+
printfn "MyComNonVisibleType instantiated!"
14+
15+
let createComInstance typeName =
16+
try
17+
let currentDomain = AppDomain.CurrentDomain
18+
let assemblyName = currentDomain.FriendlyName
19+
currentDomain.CreateComInstanceFrom(assemblyName, typeName)
20+
|> ignore
21+
with e ->
22+
printfn $"{e.Message}"
23+
24+
createComInstance "MyComNonVisibleType" // Fail!
25+
createComInstance "MyComVisibleType" // OK!
26+
27+
// </Snippet1>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net48</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="createcominstancefrom.fs" />
9+
</ItemGroup>
10+
</Project>

0 commit comments

Comments
 (0)