C
C#

help

✅ Determining a Configuration Type

AAyymoss12/11/2022
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?*/ );
}
Eero12/11/2022
If statements? Or a switch expression?
AAyymoss12/11/2022
I thought this, but it feels unnecessarily verbose. Surely there's a smarter way to infer the type?
Eero12/11/2022
Dictionary?
AAyymoss12/11/2022
Again. I don't want to make a static dict with all key and types. 😄 It's way to verbose and not scalable.
Eero12/11/2022
You don't really have any other options
AAyymoss12/11/2022
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?
Eero12/11/2022
Difficult to make any assumptions without seeing use cases and implementations and intentions
AAyymoss12/11/2022
_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.
Eero12/11/2022
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
AAyymoss12/11/2022
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.
Pphaseshift12/11/2022
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
AAyymoss12/11/2022
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.
Pphaseshift12/11/2022
so you only need a dictionary/list of configs somewhere. you can even have the enum as a property on the actual configs
AAccord12/12/2022
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.

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended Posts
❔ Social media profiles hidden information?in social media websites when i set something like my email to private or only shown for me does the❔ Deleting something from a canvas.How do I remove an entity from a canvas? I've figured out, that there is a "canvas.Children.Remove()Newtonsoft JsonConverter to replicate the serialization of the JsonObjectAttribute on an IEnumerableCan someone help me create a Newtonsoft.Json.JsonConverter that will write an IEnumerable the same w❔ static fields for struct types(1) ```cs public static class StructA { public static readonly StructA ValueA = new (..) ; } `✅ WPF FontsHow can I use custom fonts in a WPF application?❔ Is it possible to use a variable name in an object call?Forgive me if I worded it wrong(that is probably part of the problem of why I can't find information❔ Write an Aggregate```cs public override string SolvePartOne() { HashSet<House> visited = new HashS❔ Convert unicode characters in json value to Japanese or Chinese or Korean characters.How may I do this?Double check if an object is one of provided typesIs there a way to quickly check that? I mean `a` can be either TextBox or CheckBox, doesn't matter wRemove string on specified characterHi, how do I slice off a string on a specified character. For example, I have `To.Remove.This`. How ❔ postgres EF core, how to have a custom connection string config fileHey, I have a net6 class library with a `DbContext`, I want to have the connection string in a separ❔ Using methods in Grade Statistics projectWrite a method PrintoutGrades that prints the grades. Write a method Statistics that will calculate ✅ Any modern approach to getting the calling class typename aside from using the stack frame?10 years ago I used to use the stack frame to go backwards and get what class is calling my current ✅ How can I hide buttons inside a tablelayout?When i make the visible false, they still show, and if i make the visible false on both the tablelay❔ How to reverse engineer your simple console app code?Just curious. Is this possible with programs like x64dbg or... ?❔ Ef Core Constructor problemHello its my first big project with ef. I have an class with 3 attributes and one constructor that i❔ many to many database cant update the 3rd tablecant update the 3rd table who had the cust id and drink id✅ rest requests helphello i am trying to send to os command injections with RestSharp to dvwa for pentestering can someo❔ Source generator, generating type script definitions at compile timeI would like to make a automatic type script definition from code, i consider to use source generato❔ Making an async method without anything to async insidelet's say i have following scenario: ```csharp void Foo() { for(int i = 0 ; i < 10000000 ;i++){} /