我想为结构(ToString(string, IFormatprovider)
,Int32
的方法DateTime
创建一个开放委托,无论如何):
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!
它不断抛出ArgumentException,并显示消息“绑定到目标方法的错误。”。
我已经阅读了关于此主题的几乎所有stackoverflow文章,我已经尝试过和没有ref
,我在创建委托时添加并删除了null
。似乎没有任何帮助。
有没有人知道?
*编辑*
当我使用相同的方法创建自己的结构时,上面的代码(使用DateTime替换了我的MyStruct)完全正常。
public struct MyStruct
{
public string ToString(string format, IFormatProvider provider)
{
return null;
}
}
是什么让Int32
或DateTime
如此不同?
*编辑*
根据要求,我添加了一个完整的“工作”程序。我忘了提到:我正在研究.NET framework 3.5。此外,正如我之前所说,这一切都在MyStruct
上进行。除此之外,当我实现接口IFormattable
时,它也不再起作用了。
using System;
namespace OpenDelegates
{
public delegate string MyCoverter<T>(ref T from, string format, IFormatProvider provider)
where T : struct;
class Program
{
static void Main(string[] args)
{
var method = typeof(MyStruct).GetMethod("ToString", new[] { typeof(string), typeof(IFormatProvider) });
var d = Delegate.CreateDelegate(typeof(MyCoverter<MyStruct>), null, method);
method = typeof(DateTime).GetMethod("ToString", new[] { typeof(string), typeof(IFormatProvider) });
d = Delegate.CreateDelegate(typeof(MyCoverter<DateTime>), null, method);
}
}
public struct MyStruct //: IFormattable
{
public string ToString(string format, IFormatProvider provider)
{
return null;
}
}
}
*编辑*
这一切都在.NET Framework 4.x上完美运行,但这对我来说不是一个解决方案。
答案 0 :(得分:3)
我不知道具体问题的答案,但也许你可以建立自己的代表:
public static Func<object, string, IFormatProvider, string> CreateConverter<T>()
where T : struct // not really needed
{
var method = typeof(T).GetMethod("ToString", new[] { typeof(string), typeof(IFormatProvider) });
if (method == null)
{
throw new InvalidOperationException(string.Format("The type {0} does not contain a suitable ToString-method.", typeof(T).FullName));
}
var instanceParameter = Expression.Parameter(typeof(object), "instance");
var formatParameter = Expression.Parameter(typeof(string), "format");
var providerParameter = Expression.Parameter(typeof(IFormatProvider), "provider");
var convertedInstance = Expression.Convert(instanceParameter, typeof(T));
var methodCall = Expression.Call(convertedInstance, method, formatParameter, providerParameter);
var lambda = Expression.Lambda<Func<object, string, IFormatProvider, string>>(methodCall, instanceParameter, formatParameter, providerParameter);
return lambda.Compile();
}
可以像:
一样使用var d = CreateConverter<MyStruct>();
Console.WriteLine(d(new MyStruct(), "", CultureInfo.CurrentCulture));
d = CreateConverter<DateTime>();
Console.WriteLine(d(DateTime.Now, "yyyydd", CultureInfo.CurrentCulture));
编辑以确保输入的类型正确
public static Func<T, string, IFormatProvider, string> CreateConverter<T>()
{
var method = typeof(T).GetMethod("ToString", BindingFlags.Instance | BindingFlags.Public, null, new[] { typeof(string), typeof(IFormatProvider) }, null);
if (method == null)
{
throw new InvalidOperationException(string.Format("The type {0} does not contain a suitable ToString-method.", typeof(T).FullName));
}
var instanceParameter = Expression.Parameter(typeof(T), "instance");
var formatParameter = Expression.Parameter(typeof(string), "format");
var providerParameter = Expression.Parameter(typeof(IFormatProvider), "provider");
var methodCall = Expression.Call(instanceParameter, method, formatParameter, providerParameter);
var lambda = Expression.Lambda<Func<T, string, IFormatProvider, string>>(methodCall, instanceParameter, formatParameter, providerParameter);
return lambda.Compile();
}