Conversation
Notices
-
In the last few days I wished that C# had the "friend" keyword. It'd make things so much easier and less error-prone. It makes that a class can access the protected methods and fields of the friended class.
Tuesday, 20-Aug-19 19:01:31 UTC from web- Jonathan Chouinard likes this.
-
@adiwan There's a concept of "friend" libraries, so you could set the field to `protected internal` and then access it from the friend. It's useful for testing, but not sure if it would help in your situation.
-
@thismightbeauser I have read that but every available solution doesn't solve my problem. All of them need to have a setup that's not worth the hassle. I make the methods public now and I commented that no other class is allowed to use that. Background: I have screw holes and screwing tools. I intend the screw holes to manage entering and leaving the screw hole zones for interaction. The screw holes inform the screwing tool that it has entered the zone and can interact with it. I manage every interaction through interfaces instead of concrete classes because the screwing tools can inherit from an already existing class (like a standard interactable object class) and C# is boring and doesn't allow inheriting from multiple classes.
Jonathan Chouinard likes this. -
@adiwan What if you did something like setValue<T, U>(T caller, Function<T, U> valFunc) where T : InterfaceOrClassAllowed , getValue<T, U>(T caller, Function<T, U> valFunc) where T : InterfaceOrClassAllowed . The lamba tells the function where to retrieve or store the value from, and the method doesn't have to know where the value comes from, but it guarantees it came from the other object.
-
@thismightbeauser Actually forget U. U would be a fixed type and match the type of the field.
-
@adiwan The solution usually given for multiple inheritance in C# is composition. It's a little nasty though.
-
@thismightbeauser Actually no. I'm dumb. You could return whatever you wanted unless it was an Expression<Function<>> and you actually checked the expression tree before executing.
-
@adiwan Look at the call stack for the class you want to allow. That would be the runtime solution.
-
@thismightbeauser I'm too tired right now to think this through but tomorrow morning I'll give it a thorough and scrutinizing look, however Reflection is not something I want to use.
Jonathan Chouinard likes this. -
@adiwan It wouldn't be reflection it would be literally grepping the call stack to make sure the full classname you want to allow is in it.
-
@thismightbeauser So like when the runtime barfs out an error and it tells you all the steps it took to get there.
-
@thismightbeauser Additionally, you could use an Attribute to mark the class as a "friend" and search for that attribute when you traverse the call stack. That would be more agnostic.
-
@adiwan Are you coding this in Core or Framework? I remember a while ago there was a library that could add features to the language, like constraining a type to a specific Enum, which didn't exist at the time. You may find what you're looking for in there... Found it https://github.com/Fody/Fody
-
@thismightbeauser as far as I can tell the Unity adaptations of that are horribly outdated
Jonathan Chouinard likes this. -
@adiwan I don't fully understand why you'd use Unity to build what you mentioned, but it shouldn't have an impact on whether you can use it unless you are targeting something other than the .NET CLR like WebAssembly or something.
-
@thismightbeauser I use Unity for creating VR trainings at my work. So far I'm not done with my code. I'm restructuring my code constantly and I plan to introduce your proposal towards the end when I have something functional first. I'm also a bit hesitant how it will work with the packages I already use that heavily rely on reflection (Odin Inspector, Bolt). My damn "screw hole" code has about 1000 lines of neatly formatted code and my "cordless screwdriver" is at about 1200 lines. They are so big because I need to ensure nothing breaks when I toggle all of them interactable on or off.
Jonathan Chouinard likes this. -
@adiwan Thanks. The context helps. I was thinking that you were controlling real industrial machinery or something. I'm not too familiar with either Odin Inspector or Bolt so idk what will happen there.