18
mai
2007
Bug sur les extension methods
mai
2007
Prenons le code suivant :
public static class StringExtension
{
public static string Format(this string template, params object[] parameters)
{
return string.Format(template, parameters);
}
}
{
public static string Format(this string template, params object[] parameters)
{
return string.Format(template, parameters);
}
}
string s = StringExtension.Format("{0}", "toto");
ne pose pas de problème.
En revanche,
string s = "{0}".Format("toto");
génère une erreur à la compilation : « Member ‘string.Format(string, params object[])’ cannot be accessed with an instance reference; qualify it with a type name instead »
Il faut caster explicitement « toto » en object :
string s = "{0}".Format((object)"toto");
pour le plus avoir l’erreur.
MS est dessus : http://forums.microsoft.com/MSDN/showpost.aspx?postid=1629181&siteid=1