C
C#•12mo ago
SWEETPONY

How to get assembly?

I'm trying to get assembly using this: var test = AppDomain.CurrentDomain.GetAssemblies(); I don't understand why but I don't see my assembly in test but my project has reference to this assembly
14 Replies
Hazel 🌊💃
Are you saying to have an assembly A which is referenced by the running project B but A isn't in test? Assemblies aren't loaded into the domain until they're needed. You can test this by using a data type in your Program.cs from A in B, then test should contain A.
WAASUL
WAASUL•12mo ago
@ivefifthsence There is an extension on Assembly called GetReferencedAssemblies()
var references = Assembly.GetExecutingAssembly().GetReferencedAssemblies();
var references = Assembly.GetExecutingAssembly().GetReferencedAssemblies();
Mayor McCheese
Mayor McCheese•12mo ago
What are you trying to actually do?
WAASUL
WAASUL•12mo ago
Remember, assemblies only get loaded when they are used.
SWEETPONY
SWEETPONYOP•12mo ago
all I need is pass a name of assembly and then try to find all needed types
var assemblyNames = { "Operational.ReadModel.Commands" };
var types = InitPossibleCommandsTypes(assemblyNames);
var assemblyNames = { "Operational.ReadModel.Commands" };
var types = InitPossibleCommandsTypes(assemblyNames);
public Dictionary<Type, Type> InitPossibleCommandsTypes(string[] assemblyNames)
{
dictionaryTypes = new Dictionary<Type, Type>();
foreach(var assembly in AppDomain.CurrentDomain.GetAssemblies().Where(a=>assemblyNames.Contains(a.GetName().Name)))
{
var commandTypes = assembly.GetTypes().Where(t => t.BaseType?.IsGenericType == true
&& t.BaseType?.GetGenericTypeDefinition()
.IsAssignableTo(typeof(ResourceManagerPureCommand<>)) == true);
foreach(var commandType in commandTypes)
{
var method = commandType.GetMethod("ExecuteAsync");
if(method != null)
{
dictionaryTypes.Add(commandType, method.GetParameters().FirstOrDefault()!.ParameterType);
}
}
}
return dictionaryTypes;
}
public Dictionary<Type, Type> InitPossibleCommandsTypes(string[] assemblyNames)
{
dictionaryTypes = new Dictionary<Type, Type>();
foreach(var assembly in AppDomain.CurrentDomain.GetAssemblies().Where(a=>assemblyNames.Contains(a.GetName().Name)))
{
var commandTypes = assembly.GetTypes().Where(t => t.BaseType?.IsGenericType == true
&& t.BaseType?.GetGenericTypeDefinition()
.IsAssignableTo(typeof(ResourceManagerPureCommand<>)) == true);
foreach(var commandType in commandTypes)
{
var method = commandType.GetMethod("ExecuteAsync");
if(method != null)
{
dictionaryTypes.Add(commandType, method.GetParameters().FirstOrDefault()!.ParameterType);
}
}
}
return dictionaryTypes;
}
WAASUL
WAASUL•12mo ago
Are you able to find any assemblies?
SWEETPONY
SWEETPONYOP•12mo ago
I don't understand why but I can find ResourceManager.Commands but not Operational.ReadModel.Commands both were referenced in csproj
WAASUL
WAASUL•12mo ago
Are you using anything from Operational.ReadModel.Commands in your code? It can be anything, just a piece of code.
SWEETPONY
SWEETPONYOP•12mo ago
hm I understand
WAASUL
WAASUL•12mo ago
Just by giving a project a reference, it doesn't mean it will get loaded. It will only load if, you are using it.
SWEETPONY
SWEETPONYOP•12mo ago
yes I see, thanks I will use foreach(var assembly in assemblyNames.Select(Assembly.Load)) instead why do you think about it?
WAASUL
WAASUL•12mo ago
I get the idea of what you are trying to do. You are technically using code from Operational.ReadModel.Commands but not directly and that's why the assembly is not getting loaded.
SWEETPONY
SWEETPONYOP•12mo ago
yes
WAASUL
WAASUL•12mo ago
You can maybe improve the code, by only loading your own assemblies.

Did you find this page helpful?