✅ Can custom attributes use interfaces somehow ?
I have an interface named IConfiguration and I'm wondering is it possible to write a custom attribute so I can do [StaticConfiguration("Key"]var settings: Settings so that the StaticConfiguration attribute will get the config for that key from IConfiguration and parse it into the type Settings?
10 Replies
you can't place attributes on local variables
Sorry it doesn't need to be a local variable, it can be Settings settings;
it's possible to use reflection to set the value of that member variable yeah; it's also possible to get the attributes on that member variable, so you'll have to figure out how to put it all together
i'm not sure where interfaces come into play
I’m using dependency injection and I need the StaticConfiguration class to get the values from an interface of type IConfiguration
what's your end goal?
do you have something like
and you want
Settings
to be set based on something you get from dependency injection?Yeah exactly
$options
appsettings.json:
src/Foo/FooOptions.cs:
src/Foo/FooServiceCollectionExtensions.cs:
Program.cs / Startup.cs:
Bar.cs:
^
Unknown User•5d ago
Message Not Public
Sign In & Join Server To View