0

I am using a custom JsonConverter. And I need to get type of an object and set in as generic parameter.

   public class PagedResultConverter : Newtonsoft.Json.JsonConverter
    {
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            var pagedList = (IPagedList<T>)value;

            var jsonJObject = new JObject
            {
                {"TotalCount", pagedList.TotalCount}
            }

            jsonJObject.WriteTo(writer);
        }
    }

My IPagedList<T> parameter T will be type of typeof(value) But I can not set IPagedList<typeof(value)>

I need to get properties, members of interface.

How can I do this?

barteloma
  • 6,403
  • 14
  • 79
  • 173
  • 1
    You can´t. As you allready noticed `T`is a *compile-time*-type, whereas you provide the actual type at *runtime*. How should the *compiler* know what you provide at *runtime*? However you may have a look at https://stackoverflow.com/questions/232535/how-do-i-use-reflection-to-call-a-generic-method – MakePeaceGreatAgain Oct 27 '17 at 11:42
  • What are you trying to accomplish? Probably there is another way to do what you want to do. – Kote Oct 27 '17 at 11:45
  • `typeof(value)` will always be `object` because it's compile time. What you probably mean is `value.GetType()`. Doesn't help with your problem though. – nvoigt Oct 27 '17 at 11:46
  • @nvoigt `typeof(value) ` won´t even compile. – MakePeaceGreatAgain Oct 27 '17 at 11:47
  • @Kote I updated post, I need to access members of interface. – barteloma Oct 27 '17 at 11:48
  • 1
    If you own `IPagedList` - make it inherit non-generic `IPagedList` interface. Put `TotalCount` in that `IPagedList` interface, then you can just cast to `IPagedList` to get it. Otherwise - use reflection to get that `TotalCount`. – Evk Oct 27 '17 at 11:50
  • If you wanted `T` to be the type of `value`, i.e. `value.GetType()` then how could you *possibly* cast `value` to `IPagedList`? Sounds more like `value` will be *some* `IPagedList` for *some* `T` that is unknown to you. – Why don’t you make your `WriteJson` generic? – poke Oct 27 '17 at 11:52

1 Answers1

0

As I already mentioned in the comment you can´t expect the compiler to infer a type you provide at runtime. That is when you know the actual type only when executing your program, how shopuld the compiler give you the right information on pagedList beforehand?

Maybe you can introduce a nin-generic interface that your generic one inherits from:

interface IPagedList { int TotalCount { get; set; } }
interface IPagedList<T> : IPagedList { ... }

Now you can cast the de-serialized instance to the non-generic interface:

var pagedList = (IPagedList)value;

However why don´t you make your WriteJson-method generic instead and provide the type via reflection:

public override void WriteJson<T>(JsonWriter writer, Tvalue, JsonSerializer serializer)
{
    var pagedList = (IPagedList<T>)value;
}

var obj = ...
var m = typeof(MyClass).GetMethod("WriteJson").MakeGenericMethod(obj.GetType());
m.Invoke(instanceOfMyClass, new object[] { myWriter, obj, myerializer });
MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
  • He couldn't make `WriteJson` generic because then the converter won't correctly implement the abstract `WriteJson` from `Newtonsoft.Json.JsonConverter`. – Brian Rogers Oct 27 '17 at 14:29