18 Mart 2009, ÇARŞAMBA
C# nasıl bir yöntem içinde geçti genel bir tür oluşturmak için?
Nasıl başlatılamadı türü aşağıda InstantiateType<T>
benim yöntem içinde T miyim?
Hata alıyorum:'' Bir 'tür parametresi" gibi kullanılır ''. değişken T:
(REFACTORED CEVAP İÇİN AŞAĞI KAYDIRIN)
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; }
}
}
CEVAP REFACTORED:
Tüm yorumlar için teşekkürler, doğru yolda yakaladılar beni, yapmak istediğim şey şu:
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; }
}
}
CEVAP
18 Mart 2009, ÇARŞAMBA
Bu şekilde metot:
public string InstantiateType<T>(string firstName, string lastName) where T : IPerson, new()
Sonunda ek kısıtlama dikkat edin. Yöntem vücutta new
bir örnek oluşturmak:
T obj = new T();
Bunu Paylaş:
Nasıl oluşturmak statik bir yöntem içi...
Genel yöntem oluşturmak bir Numaralama...
Nasıl Java genel bir dizi oluşturmak i...
Bir döngü içinde nesne harfleri bir di...
Nasıl bir sınıf içinde bir şablon işle...