Activator.CreateInstance Help!

I am not fully understanding this Why is this an option all over the internet for getting a instance or creating one all i want to do is use the currently loaded Assembly as a Reflector as the dll i want is Assembled in its own directory i get the assembly AppDomain.CurrentDomain.GetAssemblies(); But i want to change a Boolean within that assembly but when googled there nothing but Activator.CreateInstance i tried it it seems to work but not for the currently loaded assembly its like its creating a Ghost Assembly changing the variable and thats it and doesnt effect anything even a debug log is never trigged when the bool gets changed in the Assembly i want to change in
11 Replies
ⱧØⱠɎ₴₵ⱧɎ₮₮
I even tried using a snippet on Calling a function SomeMethod using Reflection it gives an error as it doesnt find it i guess i got the somemethod from Pilot. AI
mtreit
mtreit2mo ago
It sounds like you have a fundamental misunderstanding of how reflection works or how assemblies work. What do you mean "change a bool in the assembly" ?
Jimmacle
Jimmacle2mo ago
you don't create instances of assemblies, you create instances of classes and non-static members of a class are not shared with other instances of that class, so if you create an instance and change something in it that won't change anything outside the instance you just made
ⱧØⱠɎ₴₵ⱧɎ₮₮
Oh so your saying this bool needs to be a static
namespace AssemblyB
{
public class Configuration
{
public bool mLoaderStop { get; set; } = false;
}
}
namespace AssemblyB
{
public class Configuration
{
public bool mLoaderStop { get; set; } = false;
}
}
Or the Class
Jimmacle
Jimmacle2mo ago
i mean, that's a solution another solution is getting the instance of the class you actually want to modify instead of creating a new one but if it's all public, why are you using reflection in the first place?
ⱧØⱠɎ₴₵ⱧɎ₮₮
That makes sense why everything working fine but the bool being changed
Jimmacle
Jimmacle2mo ago
randomly making things static isn't a good idea, static state should be avoided as much as possible and if you have control over this code then there's no reason to use reflection at all
ⱧØⱠɎ₴₵ⱧɎ₮₮
Yes i have control over the code. Its been forked from git with a readme of todo thats never been done.. i like to use it for working on mods What other way you suggest doing the reference as i have assemblyA < Main trying to access assemblyB thats in a folder called Client i tried just referencing the assemblyB.dll in assemblyA and Declaring it as Using assemblyBnamespace; and change the bool from there but when i attempted to change it nothing happened.. should not put AssemblyB in Client and just build it with AssemblyA and set copy local true???
mtreit
mtreit2mo ago
Build a NuGet package. Reference that. Or just ProjectReference between the two projects if they are in the same repo
ⱧØⱠɎ₴₵ⱧɎ₮₮
I used static it only used to start and stop the ModLoader
Jimmacle
Jimmacle2mo ago
I think you should do some more learning about how OOP and C# in general works

Did you find this page helpful?