C
C#•10mo 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.
V0FBU1VM
V0FBU1VM•10mo ago
@ivefifthsence There is an extension on Assembly called GetReferencedAssemblies()
var references = Assembly.GetExecutingAssembly().GetReferencedAssemblies();
var references = Assembly.GetExecutingAssembly().GetReferencedAssemblies();
Mayor McCheese
Mayor McCheese•10mo ago
What are you trying to actually do?
V0FBU1VM
V0FBU1VM•10mo ago
Remember, assemblies only get loaded when they are used.
SWEETPONY
SWEETPONYOP•10mo 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;
}
V0FBU1VM
V0FBU1VM•10mo ago
Are you able to find any assemblies?
SWEETPONY
SWEETPONYOP•10mo ago
I don't understand why but I can find ResourceManager.Commands but not Operational.ReadModel.Commands both were referenced in csproj
V0FBU1VM
V0FBU1VM•10mo ago
Are you using anything from Operational.ReadModel.Commands in your code? It can be anything, just a piece of code.
SWEETPONY
SWEETPONYOP•10mo ago
hm I understand
V0FBU1VM
V0FBU1VM•10mo 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•10mo ago
yes I see, thanks I will use foreach(var assembly in assemblyNames.Select(Assembly.Load)) instead why do you think about it?
V0FBU1VM
V0FBU1VM•10mo 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•10mo ago
yes
V0FBU1VM
V0FBU1VM•10mo ago
You can maybe improve the code, by only loading your own assemblies.
Want results from more Discord servers?
Add your server