本文整理汇总了C#中IAssemblyLoadContext.Load方法的典型用法代码示例。如果您正苦于以下问题:C# IAssemblyLoadContext.Load方法的具体用法?C# IAssemblyLoadContext.Load怎么用?C# IAssemblyLoadContext.Load使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IAssemblyLoadContext
的用法示例。
在下文中一共展示了IAssemblyLoadContext.Load方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetCompiler
public IProjectCompiler GetCompiler(TypeInformation provider, IAssemblyLoadContext loadContext)
{
// TODO: Optimize the default compiler case by using the default load context directly
var services = new ServiceProvider(_context.Services);
services.Add(typeof(IAssemblyLoadContext), loadContext);
var assembly = loadContext.Load(provider.AssemblyName);
var type = assembly.GetType(provider.TypeName);
return (IProjectCompiler)ActivatorUtilities.CreateInstance(services, type);
}
示例2: RegisterPlugin
private PluginResponseMessage RegisterPlugin(
PluginMessage message,
IAssemblyLoadContext assemblyLoadContext)
{
var registerData = message.Data.ToObject<PluginRegisterData>();
var response = new RegisterPluginResponseMessage
{
MessageName = RegisterPluginMessageName
};
var pluginId = message.PluginId;
var registerDataTypeCacheKey = registerData.GetFullTypeCacheKey();
IPlugin plugin;
Type pluginType;
if (!_pluginTypeCache.TryGetValue(registerDataTypeCacheKey, out pluginType))
{
try
{
Assembly assembly;
if (!_assemblyCache.TryGetValue(registerData.AssemblyName, out assembly))
{
assembly = assemblyLoadContext.Load(registerData.AssemblyName);
}
pluginType = assembly.GetType(registerData.TypeName);
}
catch (Exception exception)
{
response.Error = exception.Message;
return response;
}
}
if (pluginType == null)
{
response.Error = Resources.FormatPlugin_TypeCouldNotBeLocatedInAssembly(
pluginId,
registerData.TypeName,
registerData.AssemblyName);
return response;
}
else
{
// We build out a custom plugin service provider to add a PluginMessageBroker and
// IAssemblyLoadContext to the potential services that can be used to construct an IPlugin.
var pluginServiceProvider = new PluginServiceProvider(
_hostServices,
messageBroker: new PluginMessageBroker(pluginId, _sendMessageMethod));
plugin = ActivatorUtilities.CreateInstance(pluginServiceProvider, pluginType) as IPlugin;
if (plugin == null)
{
response.Error = Resources.FormatPlugin_CannotProcessMessageInvalidPluginType(
pluginId,
pluginType.FullName,
typeof(IPlugin).FullName);
return response;
}
}
Debug.Assert(plugin != null);
int resolvedProtocol;
if (!registerData.Protocol.HasValue)
{
// Protocol wasn't provided, use the plugin's protocol.
resolvedProtocol = plugin.Protocol;
}
else
{
// Client and plugin protocols are max values; meaning support is <= value. The goal in this method is
// to return the maximum protocol supported by both parties (client and plugin).
resolvedProtocol = Math.Min(registerData.Protocol.Value, plugin.Protocol);
// Update the plugins protocol to be the resolved protocol.
plugin.Protocol = resolvedProtocol;
}
_registeredPlugins[pluginId] = plugin;
response.Protocol = resolvedProtocol;
return response;
}
示例3: RegisterPlugin
private PluginResponseMessage RegisterPlugin(
PluginMessage message,
IAssemblyLoadContext assemblyLoadContext)
{
var registerData = message.Data.ToObject<PluginRegisterData>();
var response = new PluginResponseMessage
{
MessageName = RegisterPluginMessageName
};
var pluginId = message.PluginId;
var registerDataTypeCacheKey = registerData.GetFullTypeCacheKey();
IPlugin plugin;
Type pluginType;
if (!_pluginTypeCache.TryGetValue(registerDataTypeCacheKey, out pluginType))
{
try
{
Assembly assembly;
if (!_assemblyCache.TryGetValue(registerData.AssemblyName, out assembly))
{
assembly = assemblyLoadContext.Load(registerData.AssemblyName);
}
pluginType = assembly.GetType(registerData.TypeName);
}
catch (Exception exception)
{
response.Error = exception.Message;
return response;
}
}
if (pluginType == null)
{
response.Error = Resources.FormatPlugin_TypeCouldNotBeLocatedInAssembly(
pluginId,
registerData.TypeName,
registerData.AssemblyName);
return response;
}
else
{
// We build out a custom plugin service provider to add a PluginMessageBroker and
// IAssemblyLoadContext to the potential services that can be used to construct an IPlugin.
var pluginServiceProvider = new PluginServiceProvider(
_hostServices,
messageBroker: new PluginMessageBroker(pluginId, _sendMessageMethod));
plugin = ActivatorUtilities.CreateInstance(pluginServiceProvider, pluginType) as IPlugin;
if (plugin == null)
{
response.Error = Resources.FormatPlugin_CannotProcessMessageInvalidPluginType(
pluginId,
pluginType.FullName,
typeof(IPlugin).FullName);
return response;
}
}
Debug.Assert(plugin != null);
_registeredPlugins[pluginId] = plugin;
response.Success = true;
return response;
}