C
C#2y ago
Amos

✅ 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...
_configuration.VoteConfiguration.BanConfiguration.Cooldown;
_configuration.VoteConfiguration.KickConfiguration.Cooldown;
// etc
_configuration.VoteConfiguration.BanConfiguration.Cooldown;
_configuration.VoteConfiguration.KickConfiguration.Cooldown;
// etc
Let's say I've got this method..
public static TimeSpan Cooldown(VoteType voteType)
{
return new TimeSpan( /* What do I put here to get the per-voteType relevant cooldown?*/ );
}
public static TimeSpan Cooldown(VoteType voteType)
{
return new TimeSpan( /* What do I put here to get the per-voteType relevant cooldown?*/ );
}
14 Replies
ero
ero2y ago
If statements? Or a switch expression?
Amos
Amos2y ago
I thought this, but it feels unnecessarily verbose. Surely there's a smarter way to infer the type?
ero
ero2y ago
Dictionary?
Amos
Amos2y ago
Again. I don't want to make a static dict with all key and types. 😄 It's way to verbose and not scalable.
ero
ero2y ago
You don't really have any other options
Amos
Amos2y ago
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?
ero
ero2y ago
Difficult to make any assumptions without seeing use cases and implementations and intentions
Amos
Amos2y ago
_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.
ero
ero2y ago
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
Amos
Amos2y ago
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.
phaseshift
phaseshift2y ago
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
Amos
Amos2y ago
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...
var voteConfig = <someway to retreive the specific voteconfig based off of the passed enum>;
// then do
voteConfig.Cooldown;
var voteConfig = <someway to retreive the specific voteconfig based off of the passed enum>;
// then do
voteConfig.Cooldown;
The property in question here is part of the BaseConfiguration class. But some properties will not be.
phaseshift
phaseshift2y ago
so you only need a dictionary/list of configs somewhere. you can even have the enum as a property on the actual configs
Accord
Accord2y ago
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.