문제
좋아요, 이런 경우입니다.
정적 값으로 초기화 해야하는 일반적인베이스 클래스를 얻었습니다. 이 값은 제네릭베이스 클래스가로드되는 종류의 종류와 관련이 없습니다.
나는 다음과 같은 일을 할 수 있기를 원합니다.
GenericBaseclass.Initialize(AssociatedObject);
또한 다음과 같은 수업을하는 동안 :
public class DerivedClass : GenericBaseclass<int>
{
...
}
이것을 달성 할 수있는 방법이 있습니까? 나는 비 게릭베이스 클래스를 만들고 정적 방법을 거기에 넣을 수 있었지만 그 "해킹"을 좋아하지 않습니다. :)
해결책
값이 일반 기본 클래스의 유형과 관련이 없다면 일반 기본 클래스에 있어서는 안됩니다. 그것들은 완전히 별도의 클래스에 있거나 제네릭 클래스의 비 게 릭 기본 클래스에 있어야합니다.
정적 변수의 경우 유형 인수 조합마다 다른 정적 변수를 얻습니다.
using System;
public class GenericType<TFirst, TSecond>
{
// Never use a public mutable field normally, of course.
public static string Foo;
}
public class Test
{
static void Main()
{
// Assign to different combination
GenericType<string,int>.Foo = "string,int";
GenericType<int,Guid>.Foo = "int,Guid";
GenericType<int,int>.Foo = "int,int";
GenericType<string,string>.Foo = "string,string";
// Verify that they really are different variables
Console.WriteLine(GenericType<string,int>.Foo);
Console.WriteLine(GenericType<int,Guid>.Foo);
Console.WriteLine(GenericType<int,int>.Foo);
Console.WriteLine(GenericType<string,string>.Foo);
}
}
당신이하지 않는 것 같습니다 진짜 다른 정적 변수를 원합니다 T
일반적인 기본 클래스의 - 그래서 당신은 그것을 가질 수 없습니다 안에 일반적인 기본 클래스.
다른 팁
그것이 바로 당신이해야 할 일입니다. 유형 매개 변수가 있으면 유형의 각각의 다른 인스턴스화는 분리된 유형. 이로 인해 정적 변수가 분리됩니다.
유일한 해결 방법은 제네릭 클래스가 얻는 기본 클래스를 갖는 것입니다.
제휴하지 않습니다 StackOverflow