✅ Determining a Configuration Type
I'm running into a problem trying to access a specific property of a configuration type. I'm unsure if I'm having this problem due to the layout of my configuration or just part of C# I've not yet ran into. 🙂
I have an enum
VoteType
with 4 members, Kick, Ban, Map, Skip, Mute.
I have a configuration for those relevant types KickConfiguration
, BanConfiguration
, MuteConfiguration
etc these configurations inherit from BaseConfiguration
Each 'TYPE'Configuration has a inherited property Cooldown
.
In my code, I need to access this cooldown specific to the vote type.
How do I do this? In the method I need it, I have access to the VoteType
enum. I'm not sure if there's a way to infer the configuration type from this enum?
The full property 'path'(?) is...
Let's say I've got this method..
14 Replies
If statements?
Or a switch expression?
I thought this, but it feels unnecessarily verbose.
Surely there's a smarter way to infer the type?
Dictionary?
Again. I don't want to make a static dict with all key and types. 😄 It's way to verbose and not scalable.
You don't really have any other options
I tried using Generics, but I'm not too familiar with them and I believe you need to already know the type beforehand?
I suppose this is a problem with the layout.
Do you have any suggestions on how I could redo my config with this per-votetype-configuration in mind?
Difficult to make any assumptions without seeing use cases and implementations and intentions
_configuration.VotePassPercentage
This was my old format, and is now broken. 🙂
I need to get
_configuration.VoteConfiguration.<Type>.VotePassPercentage
now. Again, the enum is passed through to this method.Yeah that doesn't answer anything
That's your current setup that you asked for suggestions to change
I'm saying it's impossible to tell you how to change it without more context
I'm trying to provide that context. Can you be more specific in what context you need?
I can start dumping entire code snippets out but I can't imagine this helpful.
Are you saying the property is not on the base class? If not, what is the problem?
Passing round an enum into lots of different methods expecting them to switch or otherwise iterate the options is often not a good pattern. Just pass a single thing that is set up appropriately
Possibly my problem. I'm unsure.
I'm using the enum to pass through to functions to know what the calling vote was. But I also need to get relevant configuration results from it.
It'd be nice at the top of the function to call like...
The property in question here is part of the BaseConfiguration class. But some properties will not be.
so you only need a dictionary/list of configs somewhere. you can even have the enum as a property on the actual configs
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.