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
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" ?
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
Or the Class
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
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???
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
I think you should do some more learning about how OOP and C# in general works