Thanks to visit codestin.com
Credit goes to github.com

Skip to content

System.AppDomain F# snippets #7914

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions snippets/fsharp/System/AppDomain/AssemblyLoad/assemblyload.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// <Snippet1>
open System

let printLoadedAssemblies (domain: AppDomain) =
printfn "LOADED ASSEMBLIES:"
for a in domain.GetAssemblies() do
printfn $"{a.FullName}"
printfn ""

let myAssemblyLoadEventHandler _ (args: AssemblyLoadEventArgs) =
printfn $"ASSEMBLY LOADED: {args.LoadedAssembly.FullName}\n"

let currentDomain = AppDomain.CurrentDomain
currentDomain.AssemblyLoad.AddHandler(AssemblyLoadEventHandler myAssemblyLoadEventHandler)

printLoadedAssemblies currentDomain
// Lists mscorlib and this assembly

// You must supply a valid fully qualified assembly name here.
currentDomain.CreateInstance("System.Windows.Forms, Version, Culture, PublicKeyToken", "System.Windows.Forms.TextBox")
// Loads System, System.Drawing, System.Windows.Forms

printLoadedAssemblies currentDomain
// Lists all five assemblies


// </Snippet1>
10 changes: 10 additions & 0 deletions snippets/fsharp/System/AppDomain/AssemblyLoad/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="assemblyload.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
open System
open System.Reflection

// <Snippet1>
type MyType() =
do
printfn "\nMyType instantiated!"

let instantiateMyTypeFail (domain: AppDomain) =
// Calling InstantiateMyType will always fail since the assembly info
// given to CreateInstance is invalid.
try
// You must supply a valid fully qualified assembly name here.
domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyType")
|> ignore
with e ->
printfn $"\n{e.Message}"

let instantiateMyTypeSucceed (domain: AppDomain) =
try
let asmname = Assembly.GetCallingAssembly().FullName
domain.CreateInstance(asmname, "MyType")
|> ignore
with e ->
printfn $"\n{e.Message}"

let myResolveEventHandler _ _ =
printfn "Resolving..."
typeof<MyType>.Assembly


let currentDomain = AppDomain.CurrentDomain

// This call will fail to create an instance of MyType since the
// assembly resolver is not set
instantiateMyTypeFail currentDomain

currentDomain.add_AssemblyResolve (ResolveEventHandler myResolveEventHandler)

// This call will succeed in creating an instance of MyType since the
// assembly resolver is now set.
instantiateMyTypeFail currentDomain

// This call will succeed in creating an instance of MyType since the
// assembly name is valid.
instantiateMyTypeSucceed currentDomain

// </Snippet1>
10 changes: 10 additions & 0 deletions snippets/fsharp/System/AppDomain/AssemblyResolve/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="assemblyresolve.fs" />
</ItemGroup>
</Project>
29 changes: 29 additions & 0 deletions snippets/fsharp/System/AppDomain/BaseDirectory/adsetup.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// <SNIPPET1>
open System
open System.IO

// Create application domain setup information
let domaininfo = AppDomainSetup()
domaininfo.ConfigurationFile <- Environment.CurrentDirectory + string Path.DirectorySeparatorChar + "ADSetup.exe.config"
domaininfo.ApplicationBase <- Environment.CurrentDirectory

//Create evidence for the new appdomain from evidence of the current application domain
let adEvidence = AppDomain.CurrentDomain.Evidence

// Create appdomain
let domain = AppDomain.CreateDomain("Domain2", adEvidence, domaininfo)

// Display application domain information.
printfn $"Host domain: {AppDomain.CurrentDomain.FriendlyName}"
printfn $"Child domain: {domain.FriendlyName}\n"
printfn $"Configuration file: {domain.SetupInformation.ConfigurationFile}"
printfn $"Application Base Directory: {domain.BaseDirectory}"

AppDomain.Unload domain
// The example displays output like the following:
// Host domain: adsetup.exe
// Child domain: Domain2
//
// Configuration file: C:\Test\ADSetup.exe.config
// Application Base Directory: C:\Test
// </SNIPPET1>
10 changes: 10 additions & 0 deletions snippets/fsharp/System/AppDomain/BaseDirectory/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net48</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="adsetup.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// <SNIPPET1>
open System

//Create evidence for new appdomain.
let adevidence = AppDomain.CurrentDomain.Evidence

//Create the new application domain.
let domain = AppDomain.CreateDomain("MyDomain", adevidence)

//Display the current relative search path.
printfn $"Relative search path is: {domain.RelativeSearchPath}"

//Append the relative path.
let Newpath = "www.code.microsoft.com"
domain.AppendPrivatePath Newpath

//Display the new relative search path.
printfn $"Relative search path is: {domain.RelativeSearchPath}"

//Clear the private search path.
domain.ClearPrivatePath()

//Display the new relative search path.
printfn $"Relative search path is now: {domain.RelativeSearchPath}"

AppDomain.Unload domain
// </SNIPPET1>
10 changes: 10 additions & 0 deletions snippets/fsharp/System/AppDomain/ClearPrivatePath/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net48</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="adclearprivatepath.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// <Snippet1>
open System
open System.Runtime.InteropServices

[<ComVisible true>]
type MyComVisibleType() =
do
printfn "MyComVisibleType instantiated!"

[<ComVisible false>]
type MyComNonVisibleType() =
do
printfn "MyComNonVisibleType instantiated!"

let createComInstance typeName =
try
let currentDomain = AppDomain.CurrentDomain
let assemblyName = currentDomain.FriendlyName
currentDomain.CreateComInstanceFrom(assemblyName, typeName)
|> ignore
with e ->
printfn $"{e.Message}"

createComInstance "MyComNonVisibleType" // Fail!
createComInstance "MyComVisibleType" // OK!

// </Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net48</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="createcominstancefrom.fs" />
</ItemGroup>
</Project>
10 changes: 10 additions & 0 deletions snippets/fsharp/System/AppDomain/CreateDomain/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net48</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="setup.fs" />
</ItemGroup>
</Project>
18 changes: 18 additions & 0 deletions snippets/fsharp/System/AppDomain/CreateDomain/setup.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// <Snippet1>
open System
open System.Security.Policy

// Set up the AppDomainSetup
let setup = AppDomainSetup()
setup.ApplicationBase <- "(some directory)"
setup.ConfigurationFile <- "(some file)"

// Set up the Evidence
let baseEvidence = AppDomain.CurrentDomain.Evidence
let evidence = Evidence baseEvidence
evidence.AddAssembly "(some assembly)"
evidence.AddHost "(some host)"

// Create the AppDomain
let newDomain = AppDomain.CreateDomain("newDomain", evidence, setup)
// </Snippet1>
Loading