Skip to content

Does not handle type conversion #2

Description

@donald-fraser

I am getting the following exception thrown, line 39 of DataAnnotationsValidator.cs (name space: ReHackt.Extensions.Options.Validation)

InvalidOperationException: Method may only be called on a Type for which Type.IsGenericParameter is true.
stack trace:

   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
   at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
   at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index)
   at ReHackt.Extensions.Options.Validation.DataAnnotationsValidator.TryValidateObjectRecursive[T](T obj, List`1 results, ISet`1 validatedObjects, IDictionary`2 validationContextItems)
   at ReHackt.Extensions.Options.Validation.DataAnnotationsValidator.TryValidateObjectRecursive[T](T obj, List`1 results, ISet`1 validatedObjects, IDictionary`2 validationContextItems)
   at ReHackt.Extensions.Options.Validation.DataAnnotationsValidator.TryValidateObjectRecursive[T](T obj, List`1 results, ISet`1 validatedObjects, IDictionary`2 validationContextItems)
   at ReHackt.Extensions.Options.Validation.DataAnnotationsValidator.TryValidateObjectRecursive[T](T obj, List`1 results, IDictionary`2 validationContextItems)
   at ReHackt.Extensions.Options.Validation.DataAnnotationsValidateRecursiveOptions`1.Validate(String name, TOptions options)
   at Microsoft.Extensions.Options.OptionsFactory`1.Create(String name)

It so happens that the object it is attempting to validate uses a TypeConverter attribute. In essence the the Json looks like:

{
  "MessageBroker": {
    "MessageBusUrl": "rabbitmq://localhost:5672/",
    "Territory": "USA"
  }
}

where Territory is not a string.
The options class looks like:

public class MessageBrokerConfiguration
{
        public const string Section = "MessageBroker";

        [Uri]
        public Uri MessageBusUrl { get; set; }

        [Required]
        public TerritoryWrapper Territory { get; set; }
}

Class TerritoryWrapper has a TypeConverter attribute allowing it to be constructed from a string

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghelp wantedExtra attention is needed

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions