-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add extension methods to IEnumerable<OptionsDescriptor<T>> #1787
Comments
I'm ready to do this one now, and I can submit a PR for it. |
It looks like extensions to IEnumerable<OptionDescriptor> aren't going to provide the desired result. For example this method: public static TInstance InstanceOf<TInstance>([NotNull] this
IEnumerable<OptionDescriptor<TInstance>> descriptors) Called by this code: var formatter = new MvcOptions().InputFormatters.InstanceOf<JsonInputFormatter>(); Will produce a CS1929 compilation error: The extension method requires a receiver of type I would like to suggest the following (for both input and output descriptors): public static TInstance InstanceOf<TInstance>([NotNull] this
IEnumerable<InputFormatterDescriptor> descriptors) |
You can alternatively define the e.g. public class InputFormatterDescriptor : OptionDescriptor<IInputFormatter>,
IOptionDescriptor<IInputFormatter>
{
...
} and public interface IOptionDescriptor<out TOption>
{
Type OptionType { get; }
TOption Instance { get; }
} and public class OptionDescriptor<TOption> : IOptionDescriptor<TOption>
{
....
} and finally public static TInstance InstanceOf<TInstance>([NotNull] this IEnumerable<IOptionDescriptor<object>> descriptors)
{
return default(TInstance);
} |
Perfect ... this will work for public static void RemoveTypesOf<TInstance>([NotNull] this
IList<IOptionDescriptor<object>> descriptors) Will result in a compilation error when called by this method: new MvcOptions().InputFormatters.RemoveTypesOf<JsonInputFormatter>(); Because it can't convert from Instead, public static void RemoveTypesOf<TInstance, TOptionDescriptor>([NotNull] this
IList<TOptionDescriptor> descriptors)
where TOptionDescriptor : IOptionDescriptor<object> And it will need to be called with an extra type argument, like so: new MvcOptions().InputFormatters.RemoveTypesOf<JsonInputFormatter,
InputFormatterDescriptor>(); Which is not very intuitive. Would you like me to add a |
It is currently rather cumbersome to configure options in any of the lists in MvcOptions. The user has to write selector code to get to the descriptor objects, and then get the actual instance. For example
Instead we should have a set of extension methods allowing easy access to the descriptors/instances
Here is an initial suggestion - @rynowak lets discuss and update
perhaps we should also consider a replace in place extension, so a customized options can come in the exact same spot the one it replaced is in.
#1744
The text was updated successfully, but these errors were encountered: