【问题标题】:Creating new method c# methodname<Variable>(Variable)创建新方法 c# methodname<Variable>(Variable)
【发布时间】:2014-10-09 09:29:02
【问题描述】:

我在 c# 中找到了类似 ReadAsAsync&lt;T&gt;(this HttpContent content); 的一些方法,但我不知道这是什么方法,我的头上会弹出问题

"可以创建这样的方法 'methodName&lt;varible&gt;(variable){}' 这个方法是否存在于某处?”

例如:

public void methodName<string getText>(string text)
{
    getText = text;
} 

当我调用方法时:

string sampleText;
methodName<sampleText>("Hello World");

所以sampleText 的值将变为"Hello World"

我知道这种方法没用,因为你可以这样设置sampleText的值

string sampleText = "";

但我只是想做一些实验,谢谢。

【问题讨论】:

    标签: c# methods


    【解决方案1】:

    我在c#中找到了类似ReadAsAsync&lt;T&gt;(this HttpContent content);这样的方法,但我不知道那是什么方法,我的头上会弹出问题

    这是一个通用方法。你可以通过指定你想要的类型来调用它:

    Foo result = await response.Content.ReadAsAsync<Foo>();
    

    您可以在 MSDN 上阅读更多相关信息:Generics

    “可以创建像这样的方法 'methodName(variable){}' 并且该方法是否存在于某个地方?”

    不,您尝试做的事情是不可能的。 &lt;...&gt; 之间的内容是类型,而不是变量。

    【讨论】:

    • 也就是说method&lt;type&gt;只是用来判断方法的类型?
    • @LeoSarmiento type 可以是方法中某个变量的类型,方法的任何参数/返回类型。此外,它可以用作从该方法调用的某个泛型方法的类型参数。
    • @MadSorcerer 现在我知道了,谢谢您的回复。
    【解决方案2】:

    正如 Thomas Levesque 所说,ReadAsAsync&lt;T&gt;(this HttpContent content) 是一种通用方法,它可以使用不同的类型进行操作,具体取决于 T 类型参数。

    所以sampleText的值会变成“Hello World”。

    如果这是您要查找的内容,则应使用 ref 参数。

    public void methodName(string text, ref string getText)
    {
        getText = text;
    } 
    

    使用方法:

    string sampleText;
    methodName("Hello World", ref sampleText);
    

    【讨论】:

    • 酷,我可以用ref代替return来设置变量的值,谢谢你的信息。
    • @LeoSarmiento 是的,你可以,但要小心。尽可能使用return
    【解决方案3】:

    您正在查看的是Generic Method。它们用于重用代码库中包含的逻辑,您在这些尖括号之间看到的是所谓的Type Parameter

    Type Parameters 用于return 指定的Type,或者用于指定参数的类型。

    例如,假设我们要获取名为 User 的类的属性名称

    public IEnumerable<string> GetUserProperties()
    {
        return typeof(User).GetProperties().Select(property => property.Name);
    }
    
    public class User
    {
        public string UserId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
    

    上面代码的问题是我们不能将它重用于其他类型,假设我们还想获取名为SchoolType 的属性,我们将不断创建新方法来获取任何给定 Type 的属性

    public IEnumerable<string> GetSchoolProperties()
    {
        return typeof(School).GetProperties().Select(property => property.Name);
    }
    
    public class School
    {
        public string SchoolId { get; set; }
        public string Name { get; set; }
    }
    

    为了解决这个问题,我们使用Generic Method,这种方法不仅限于一个Type(尽管可以将约束应用于类型参数,但它们暂时超出了范围,请尝试先把你的想法包起来)

    void Main()
    {
        User user = new User 
        {
            FirstName = "Aydin",
            LastName = "Aydin",
            UserId = Guid.NewGuid().ToString()
        };
    
        School school = new School
        {
            SchoolId = Guid.NewGuid().ToString(),
            Name = "Aydins school"
        };
    
        var userProperties = GetProperties(user);
        var schoolProperties = GetProperties(school);
    
        Console.WriteLine ("Retrieving the properties on the User class");
        foreach (var property in userProperties)
        {
            Console.WriteLine ("> {0}", property);
        }
    
        Console.WriteLine ("\nRetrieving the properties on the School class");
        foreach (var property in schoolProperties)
        {
            Console.WriteLine ("> {0}", property);
        }
    }   
    
    public static IEnumerable<string> GetProperties<T>(T t)
    {
        return t.GetType().GetProperties().Select(property => property.Name);
    }
    
    public class User
    {
        public string UserId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
    
    public class School
    {
        public string SchoolId { get; set; }
        public string Name { get; set; }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-22
      • 2017-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-19
      • 2023-04-07
      • 1970-01-01
      相关资源
      最近更新 更多