在C#中,如何实例化一个方法内传递的泛型类型? [英] In C#, how to instantiate a passed generic type inside a method?

查看:77
本文介绍了在C#中,如何实例化一个方法内传递的泛型类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在我的 InstantiateType< T> 方法中实例化类型T?



出现错误:'T'是一个'类型参数',但用作'变量'。

(SCROLL DOWN FOR REFACTORED ANSWER)



  using System; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;

名称空间TestGeneric33
{
类程序
{
static void Main(string [] args)
{
容器容器= new Container();
Console.WriteLine(container.InstantiateType< Customer>(Jim,Smith));
Console.WriteLine(container.InstantiateType< Employee>(Joe,Thompson));
Console.ReadLine();



public class Container
{
public T InstantiateType< T>(string firstName,string lastName)其中T:IPerson
{
T obj = T();
obj.FirstName(firstName);
obj.LastName(lastName);
return obj;
}

}

公共接口IPerson
{
字符串FirstName {get;组; }
字符串姓氏{get;组; }
}

public class Customer:IPerson
{
public string FirstName {get;组; }
public string LastName {get;组; }
public string Company {get;组; }
}

public class Employee:IPerson
{
public string FirstName {get;组; }
public string LastName {get;组; }
public int EmployeeNumber {get;组; }
}
}



REFACTORED ANSWER:



感谢所有评论,他们让我走上了正轨,这就是我想要做的:

c $ c>使用系统;

名称空间TestGeneric33
{
类程序
{
static void Main(string [] args)
{
容器容器= new Container();
Customer customer1 = container.InstantiateType< Customer>(Jim,Smith);
Employee employee1 = container.InstantiateType< Employee>(Joe,Thompson);
Console.WriteLine(PersonDisplayer.SimpleDisplay(customer1));
Console.WriteLine(PersonDisplayer.SimpleDisplay(employee1));
Console.ReadLine();


$ b $ public class Container
{
public T InstantiateType< T>(string firstName,string lastName)其中T:IPerson,new )
{
T obj = new T();
obj.FirstName = firstName;
obj.LastName = lastName;
return obj;
}
}

公共接口IPerson
{
字符串FirstName {get;组; }
字符串姓氏{get;组; }
}

public class PersonDisplayer
{
private IPerson _person;

public PersonDisplayer(IPerson person)
{
_person = person;
}

public string SimpleDisplay()
{
return String.Format({1},{0},_person.FirstName,_person.LastName) ;


public static string SimpleDisplay(IPerson person)
{
PersonDisplayer personDisplayer = new PersonDisplayer(person);
return personDisplayer.SimpleDisplay();
}
}

public class Customer:IPerson
{
public string FirstName {get;组; }
public string LastName {get;组; }
public string Company {get;组; }
}

public class Employee:IPerson
{
public string FirstName {get;组; }
public string LastName {get;组; }
public int EmployeeNumber {get;组; }
}
}


解决方案

声明你的方法是这样的:
$ b $ pre $ public string InstantiateType< T>(string firstName,string lastName)
where T: IPerson,new()

注意最后的附加约束。然后在方法体中创建一个 new 实例:

  T obj =新的T(); 


How can I instantiate the type T inside my InstantiateType<T> method below?

I'm getting the error: 'T' is a 'type parameter' but is used like a 'variable'.:

(SCROLL DOWN FOR REFACTORED ANSWER)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestGeneric33
{
    class Program
    {
        static void Main(string[] args)
        {
            Container container = new Container();
            Console.WriteLine(container.InstantiateType<Customer>("Jim", "Smith"));
            Console.WriteLine(container.InstantiateType<Employee>("Joe", "Thompson"));
            Console.ReadLine();
        }
    }

    public class Container
    {
        public T InstantiateType<T>(string firstName, string lastName) where T : IPerson
        {
            T obj = T();
            obj.FirstName(firstName);
            obj.LastName(lastName);
            return obj;
        }

    }

    public interface IPerson
    {
        string FirstName { get; set; }
        string LastName { get; set; }
    }

    public class Customer : IPerson
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Company { get; set; }
    }

    public class Employee : IPerson
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int EmployeeNumber { get; set; }
    }
}

REFACTORED ANSWER:

Thanks for all the comments, they got me on the right track, this is what I wanted to do:

using System;

namespace TestGeneric33
{
    class Program
    {
        static void Main(string[] args)
        {
            Container container = new Container();
            Customer customer1 = container.InstantiateType<Customer>("Jim", "Smith");
            Employee employee1 = container.InstantiateType<Employee>("Joe", "Thompson");
            Console.WriteLine(PersonDisplayer.SimpleDisplay(customer1));
            Console.WriteLine(PersonDisplayer.SimpleDisplay(employee1));
            Console.ReadLine();
        }
    }

    public class Container
    {
        public T InstantiateType<T>(string firstName, string lastName) where T : IPerson, new()
        {
            T obj = new T();
            obj.FirstName = firstName;
            obj.LastName = lastName;
            return obj;
        }
    }

    public interface IPerson
    {
        string FirstName { get; set; }
        string LastName { get; set; }
    }

    public class PersonDisplayer
    {
        private IPerson _person;

        public PersonDisplayer(IPerson person)
        {
            _person = person;
        }

        public string SimpleDisplay()
        {
            return String.Format("{1}, {0}", _person.FirstName, _person.LastName);
        }

        public static string SimpleDisplay(IPerson person)
        {
            PersonDisplayer personDisplayer = new PersonDisplayer(person);
            return personDisplayer.SimpleDisplay();
        }
    }

    public class Customer : IPerson
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Company { get; set; }
    }

    public class Employee : IPerson
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int EmployeeNumber { get; set; }
    }
}

解决方案

Declare your method like this:

public string InstantiateType<T>(string firstName, string lastName) 
              where T : IPerson, new()

Notice the additional constraint at the end. Then create a new instance in the method body:

T obj = new T();    

这篇关于在C#中,如何实例化一个方法内传递的泛型类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆