Allow Nullables in Attributes #991
Unanswered
TonyValenti
asked this question in
General
Replies: 2 comments
-
The limitation on the types allowed to be embedded in attribute metadata comes from the CLR and there isn't much that the compiler can do to overcome it directly. As such you might want to take up the request there. However, since C# 1.0 there has been a different pattern for using optional values in attributes and that has been named properties. Why is this not sufficient here? [AttributeUsage(AttributeTargets.Class, AllowMultiple =false)]
public class StartupExtensionOptionsAttribute : Attribute {
private bool? _intializeRequired;
private long? _intializePriority;
private long? _shutdownPriority;
public bool StartupRequired { get; private set; }
public long StartupPriority { get; private set; }
public bool InitializeRequired {
get => _initializeRequired ?? StartupRequired;
set => _initializeRequired = value;
}
public long InitializePriority {
get => _initializePriority ?? StartupPriority;
set => _initializePriority = value;
}
public long ShutdownPriority {
get => _shutdownPriority ?? -StartupPriority;
set => _shutdownPriority = value;
}
public StartupExtensionOptionsAttribute(bool startupRequired, long startupPriority) {
StartupRequired = startupRequired;
StartupPriority = startupPriority;
}
}
// usage:
[StartupExtensionOptions(true, 100, InitializeRequired = true)] |
Beta Was this translation helpful? Give feedback.
0 replies
-
Is this included in the request for constructed generic types to be used as attribute arguments? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi! I want to be able to have nullable parameters on my attributes but it is not allowed.
Beta Was this translation helpful? Give feedback.
All reactions