当前位置:网站首页>CLR via C reading notes - loading and AppDomain
CLR via C reading notes - loading and AppDomain
2022-06-29 10:31:00 【zlbcdn】
### scene
The company has a project , need TeamA And TeamB Work together ( Or two companies decide to develop a program together ), Both are developed according to the agreed interface .TeamA Your program needs to be loaded TeamB Development methods , In this way, the whole software can be completed . in addition ,TeamA The program needs to be set TeamB Security permissions and other basic settings of the plug-in , avoid TeamB The plug-in of is broken TeamA Developed programs . As shown in the figure below :
namely , Both need you , But I have to limit you !
### Solution :
stay C# You can use AppDomain Form a safe area , take TeamB To isolate the plug-ins . And then use C# Loading function , Load the plug-in , Then run the methods in the plug-in ! Security and isolation are realized !
### Code
Define interface first . The interface documents are as follows :
#### Interface definition
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HostSDK
{
public interface IAddIn
{
string DoSometing(int x);
}
}
A simple interface agreed by both parties . Here is TeamB The concrete realization of :
#### Plug in implementation
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HostSDK;
namespace AddInType
{
public class PlugInTeamB: MarshalByRefObject, IAddIn
{
public string DoSometing(int x)
{
return "B Add in method :" + 2 * x + "\n DoSomething The method is AppDomain by :" + AppDomain.CurrentDomain.FriendlyName;
}
}
}
A very simple implementation , But be careful , At this point, the plug-in inherits MarshalByRefObject Base class .
#### The actual call
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using HostSDK;
namespace ReflectionDemo
{
class Program
{
public static void Main(string[] args)
{
// Get the path of the add in
string AddInDir = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
// Show the current AppDomain
Console.WriteLine(" At present AppDomain by :"+ AppDomain.CurrentDomain.FriendlyName);
// First step (1): Under a certain path , Find all of them first DLL, Then filter according to the conditions
string[] AddInAssemblies = Directory.GetFiles(AddInDir, "*.dll");
foreach (string item in AddInAssemblies)
{
Console.WriteLine(" Name of the add in to be found :{0}",item);
}
// First step (2): Filter by criteria
List<Type> AddInType = new List<Type>();
List<string> assemblyList = new List<string>();
foreach (string item in AddInAssemblies)
{
Assembly AddInAssembly = Assembly.LoadFrom(item);
foreach (Type t in AddInAssembly.GetExportedTypes())
{
if (t.IsClass && typeof(IAddIn).IsAssignableFrom(t))
{
AddInType.Add(t);
assemblyList.Add(AddInAssembly.FullName);
}
}
}
// First step (3): Show what has been found DLL Related information
Console.WriteLine(" After finding , Already found :"+ assemblyList.Count+", As follows :");
foreach (string item in assemblyList)
{
Console.WriteLine(" What has been found assembly Information name :"+item);
}
// The second step : Create a new AppDomain, And in the new AppDomain Create instance in , And call related methods
AppDomain sandBoxAppDomain = AppDomain.CreateDomain("SandBoxAppDomain", null, null);
// new AppDomain Create instance in , And call related methods
IAddIn addInB=(IAddIn)sandBoxAppDomain.CreateInstanceAndUnwrap(assemblyList[0], "AddInType.PlugInTeamB");
Console.WriteLine(" call TeamB Methods , The return information of the method is as follows :"+addInB.DoSometing(2));
// The third step : uninstall AppDomain
AppDomain.Unload(sandBoxAppDomain);
Console.Read();
}
}
}
#### Actual output 
#### The code download
Link later -Mark
### explain
stay “ Plug in implementation ” in , The code is :
public class PlugInTeamB: MarshalByRefObject, IAddIn
{
public string DoSometing(int x)
.....
}
It does MarshalByRefObject Base class , So the final output is :“DoSomething The method is AppDomain by :SandBoxAppDomain”
If the base class is removed , Then add Serializable Of Attribute, The output will be :“DoSomething The method is AppDomain by :ReflectionDemo.exe”
This involves passing by value or by reference
#### summary
Use the small... Above demo, You can create a small “ Hot plug ” Isolation procedure for . More to the point , Third parties can be specified DLL The path to load
边栏推荐
- PGP在加密技术中的应用
- Analysis on the specific execution process of an insert statement in MySQL 8.0 (2)
- L1-009 N个数求和 (20 分)
- QGIS mapping
- How to quickly complete disk partitioning
- Beautiful ruins around Kiev -- a safe guide to Chernobyl!
- BUUCTF--reverse2
- std::unique_ptr<T>与boost::scoped_ptr<T>的特殊性
- 走迷宫 bfs 中等+——最后的编程挑战
- Maze walking BFS medium + -- the last programming challenge
猜你喜欢

How to quickly complete disk partitioning

Ce projet Open source est super wow, des photos manuscrites sont générées en ligne

qgis制图

Simulation problem of two stacks

Nacos environmental isolation

IIS server related error

2021年团体程序设计天梯赛-模拟赛

Comment terminer rapidement une partition de disque

Vmware的下载与安装(基本思路+详细过程)

Downloading and installing VMware (basic idea + detailed process)
随机推荐
这个开源项目超哇塞,手写照片在线生成
We can't tell the difference between distributed and cluster. Let's tell the story of two cooks cooking
Summary after the 2009 ICPC Shanghai regional competition
mysql 8.0 一条insert语句的具体执行流程分析(二)
Simulation problem of two stacks
Power strings [KMP cycle section]
September 17, 2020 gateway business process has two tasks: referer certification and non commodity Templating
2019.11.20 training summary
October 17, 2020: question brushing 1
L2-3 这是二叉搜索树吗?-题解超精彩哦
Acwing271 [teacher Yang's photographic arrangement] [linear DP]
《CLR via C#》读书笔记-加载与AppDomain
std::make_ shared<T>/std::make_ Unique < T> and std:: shared_ ptr<T>/std::unique_ The difference and relation between PTR < t >
51nod1277 maximum value in string [KMP]
攻防世界-Re-insfsay
2019.11.13 training summary
Common usage of LINQ in C #
Call another interface button through win32API
Analyze in detail the PBOT mining virus family behavior and the principle of exploited vulnerabilities, and provide detailed protection suggestions for the blue army
Related problems of pointer array, array pointer and parameter passing