c# - 在 C# 中,如何在方法中实例化传递的泛型类型?

标签 c# generics

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

我收到错误:'T' 是一个“类型参数”,但像“变量”一样使用。:

(向下滚动查看重构的答案)

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; }
    }
}

修改后的答案:

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

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; }
    }
}

最佳答案

像这样声明你的方法:

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

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

T obj = new T();    

关于c# - 在 C# 中,如何在方法中实例化传递的泛型类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/658951/

相关文章:

c# - 不知何故MySQL在运行后突然无法执行查询

c# - Linq Lambda Where 子句在 where 子句中

c# - 如何在asp.net 中获取当前windows 用户?

Java泛型键盘输入

java - 在返回与其对象相同类型的值的 final方法中使用泛型

c# - 避免显式泛型类型 C#

C# - Kerosene ORM - 找不到默认引擎和连接字符串

java - 如何在java中处理通用列表List<MyClass>以特定方式获取结果集

java - 如何在抽象方法中使用泛型 EnumMap 作为参数

c# - 从基类方法返回派生类型