serializer 클래스에 대한 새로운 일반 Serialize 메서드를 추가하고 있습니다. 그리고 오래된 하나는 쓸모가 없어야합니다.ObsoleteAttribute 및 Generic 메서드
You have called old Serialize method
You have called old Serialize method
You have called new Serialize<T> method
그러나 결과는 다음과 같습니다 :
[XmlRoot("a")]
public class A
{
[XmlElement("field")]
public string Field;
}
class Program
{
static void Main(string[] args)
{
A a = new A();
a.Field = "filed value";
Console.WriteLine(Serialize(a));//this calls generic version !?
Console.WriteLine();
Console.WriteLine(Serialize(null));//ok
Console.WriteLine();
Console.WriteLine(Serialize<A>(a));//ok, we explicitly called generic version
}
[Obsolete("Use Serialize<T> instead")]
public static string Serialize(Object pObject)
{
return "You have called old Serialize method";
}
public static string Serialize<T>(T value)
{
return "You have called new Serialize<T> method";
}
}
예상 결과는해야
You have called new Serialize<T> method
You have called old Serialize method
You have called new Serialize<T> method
가 왜 직렬화 (a)는 일반 버전을 호출? 더 이상 쓸모없는 일반 메소드에 버그가 있습니까? 당신이
object
에
a
캐스팅 경우
유형은 [컴파일시에 유추]됩니다 얻을 것이다 (http://stackoverflow.com/a/4976972/11683). – GSerg
과부하 해결 규칙은 약간 불투명 할 수 있습니다. 그러나 이것은 분명합니다. 실제로 Object 로의 변환을 통해 인수 유형과의 정확한 일치를 선호합니다. –
:/우리는 메서드 이름을 변경해야 할 것 같습니다 .. 또는 다른 네임 스페이스로 이동하십시오 .. –