C
C#8mo ago
Alex

Static property override

I have a base class BaseOptions and it has name value, that I will override in each child class. Base Class
public (class/interface) BaseOptions
{
string Name
}
public (class/interface) BaseOptions
{
string Name
}
Child Class
public class ApplicationOptions:BaseOptions
{
public required string ClientBaseUrl { get; set; }
// override string Name
}
public class ApplicationOptions:BaseOptions
{
public required string ClientBaseUrl { get; set; }
// override string Name
}
Usage
builder.Services.Configure<ApplicationOptions>(builder.Configuration.GetSection(ApplicationOptions.Name));
builder.Services.Configure<ApplicationOptions>(builder.Configuration.GetSection(ApplicationOptions.Name));
What should I use to create structure like above?
7 Replies
Omnissiah
Omnissiah8mo ago
is this static? because i see no static (also static is kind of outside inheritance -- which i wouldn't even use in this case) ((although still you should be able to use new to override a member))
Alex
AlexOP8mo ago
This is pseudo code, I show what I want to achieve I'm asking for a way to do it, because I don't know how
Alen Alex
Alen Alex8mo ago
as apprendeur said, static lies outside the concept of oops, so you can't achieve dynamic configuration binding like this unless you instantate it as an object... 2 way I can suggest is... 1) Use reflection to get the class name and the classname can be the one that key 2) You can use Attributes and play along a bit with it
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View
Alex
AlexOP8mo ago
That's a good suggestion, I will change it. For example I created abstract class OptionsBase that has static field Name
public abstract class OptionsBase
{
public static string Name;
}
public abstract class OptionsBase
{
public static string Name;
}
Then I inherit ApplicationOptions from it
public class ApplicationOptions:OptionsBase
{
// public const string Name = "Application"; <- This is what I'm using right now

// How to override static string Name from OptionsBase?
public required string ClientBaseUrl { get; set; }
}
public class ApplicationOptions:OptionsBase
{
// public const string Name = "Application"; <- This is what I'm using right now

// How to override static string Name from OptionsBase?
public required string ClientBaseUrl { get; set; }
}
How can I override override static property Name that is in OptionsBase class. Should I create a constructor, will it work with .AddOptions? The have name in each Options class that I can use in .BindConfiguration. The name is the same as section name in appsettings.json
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View
Alex
AlexOP8mo ago
now I get it, thank you
Want results from more Discord servers?
Add your server