mardi 18 avril 2017

Open delegate to ToString method of struct

I want to create an open delegate to the method ToString(string, IFormatprovider) of a struct (Int32, DateTime, whatever):

public delegate string MyCoverter(ref DateTime from, string format, IFormatProvider provider);
...
var method = typeof(DateTime).GetMethod("ToString", new[] { typeof(string), typeof(IFormatProvider)}); // Works!
var d= Delegate.CreateDelegate(typeof(MyCoverter), null, method); // Exception!

It keeps throwing the ArgumentException with message "Error binding to target method.".

I've read almost all stackoverflow articles on this subject, I have experimented with and without ref, I have added and removed the null when creating the delegate. Nothing seems to help.

Has anyone any clue?





Aucun commentaire:

Enregistrer un commentaire