2016-12-16 3 views
-5

Ist es möglich, eine generische C# Methode zu schreiben, die wie folgt aussehen wird:Allgemein Typ, bei dem T kann alles sein

MyClass.Function1<string>() 
MyClass.Function1<IEnumerable<string>>() 
MyClass.Function1<IDictionary<string, string>>() 
MyClass.Function1<IDictionary<string, IEnumerable<string>>>() 

T

T = string 
T = IEnumerable<string> 
T = IDictionary<string, string> 
T = IDictionary<string, IEnumerable<string>> 

Zweite Frage

kann alles sein Kann ich eine statische Methode erstellen, hängt nicht vom Typ ab?

string GetSth().Function1(); 
IEnumerable<string> GetSth().Function1() 
IDictionary<string, string> GetSth().Function1() 
IDictionary<string, IEnumerable<string>> GetSth().Function1() 
+1

Was wird 'Function1' mit dem Argument type machen? – Lee

+1

Klingt so, als müssten Sie nur ein Intro-Tutorial zu Generika lesen und eine Frage stellen, nachdem Sie auf ein spezifischeres Problem gestoßen sind, als Sie versuchten, Ihre Methode zu implementieren. Ja, es kann Ihnen möglicherweise erlauben, beides zu tun, aber es hängt davon ab, wie genau Sie dies tun wollen und was die Methode eigentlich tun soll. – Servy

+1

Stellen Sie ** eine ** Frage pro Frage, nicht zwei. – Amy

Antwort

1

für die erste Frage eine generische Klasse definieren mit denen gemeinsame Schnittstelle zu wie IEnumerable oder ohne where-Klausel:

public class MyClass 
{ 
    public static string Function1<T>() 
    { 
     return typeof(T).FullName; 
    } 
    public static string Function2<T>() where T : IEnumerable 
    { 
     return typeof(T).FullName; 
    } 
} 

Und Zum zweiten definieren eine Extension Methods:

public static class MyClass_Extensions 
{ 
    public static string StaticMethod1(this object obj) 
    { 
     return obj?.ToString(); 
    } 

    public static string StaticMethod2(this IEnumerable obj) 
    { 
     return obj?.ToString(); 
    } 
} 

So können wir diese Testmethode schreiben:

class Program 
{ 
    static void Main(string[] args) 
    { 
     Console.WriteLine(MyClass.Function1<string>()); 
     Console.WriteLine(MyClass.Function1<IEnumerable<string>>()); 
     Console.WriteLine(MyClass.Function1<IDictionary<string, string>>()); 
     Console.WriteLine(MyClass.Function1<IDictionary<string, IEnumerable<string>>>()); 

     Console.WriteLine(MyClass.Function2<string>()); 
     Console.WriteLine(MyClass.Function2<IEnumerable<string>>()); 
     Console.WriteLine(MyClass.Function2<IDictionary<string, string>>()); 
     Console.WriteLine(MyClass.Function2<IDictionary<string, IEnumerable<string>>>()); 

     // Static Method: 
     var strObj = "string"; 
     Console.WriteLine(strObj.StaticMethod1()); 
     Console.WriteLine(strObj.StaticMethod2()); 

     IEnumerable<string> listObj = new List<string>(); 
     Console.WriteLine(listObj.StaticMethod1()); 
     Console.WriteLine(listObj.StaticMethod2()); 

     IDictionary<string, string> dicObj = new Dictionary<string, string>(); 
     Console.WriteLine(dicObj.StaticMethod1()); 
     Console.WriteLine(dicObj.StaticMethod2()); 

     IDictionary<string, IEnumerable<string>> dicLisObj = new Dictionary<string, IEnumerable<string>>(); 
     Console.WriteLine(dicLisObj.StaticMethod1()); 
     Console.WriteLine(dicLisObj.StaticMethod2()); 
    } 
} 
Verwandte Themen