.NET의 "개방형"은 정확히 무엇입니까? [복제]
이 질문에는 이미 답변이 있습니다.
- 제네릭-개방 및 폐쇄 형 생성 유형 3 답변
나는 Asp.Net MVC 수업 을 겪고 컨트롤러에 대한 액션으로 자격을 얻는 방법에 대해
- "공개 일반 유형" 이 없어야합니다.
나는 제네릭을 다소 이해하고 어느 정도 사용합니다.
- .Net에서 공개 일반 유형 은 무엇입니까?
- 닫힌 제네릭 형식 과 같은 것이 있습니까?
- 개방형 제네릭 형식 은 자주 사용되지 않는 용어입니다. 무엇과 혼동됩니까?
C # 언어는 열린 형식을 형식 인수 또는 알 수없는 형식 인수로 정의 된 일반 형식 인 형식으로 정의합니다.
모든 유형은 개방형 또는 폐쇄 형으로 분류 할 수 있습니다. 개방형는 형식 매개 변수를 포함하는 유형입니다. 더 구체적으로:
- 유형 매개 변수는 개방 유형을 정의합니다.
- 배열 유형은 요소 유형이 개방형 인 경우에만 개방형입니다.
- 구축 유형 과 그 유형 인수의 경우에만 하나 이상이있는 경우 개방형입니다 개방형 . 구성된 중첩 종류 만있는 경우에는 하나보다 그 형태 인수 또는 함유 타입 (들)의 형태 인자 개방형이면 개방형이다.
밀폐형은 개방형하지 않는 타입이다.
따라서 T
, List<T>
그리고 Dictionary<string,T>
, 그리고 Dictionary<T,U>
모든 유형은 오픈 ( T
및 U
형태 인수이다) 반면 List<int>
및 Dictionary<string,int>
폐쇄 유형이다.
관련 개념이 있습니다. 바인딩되지 않은 제네릭 형식 은 지정되지 않은 형식 인수가있는 제네릭 형식입니다. 바인딩되지 않은 형식은 다른 식에서 사용할 typeof()
수 없으며 인스턴스화하거나 메서드를 호출 할 수 없습니다. 예를 들어, List<>
및 Dictionary<,>
언 바운드 종류가 있습니다.
개방형과 비 결합 형의 미묘한 차이점을 명확히하려면 :
class Program {
static void Main() { Test<int>(); }
static void Test<T>() {
Console.WriteLine(typeof(List<T>)); // Print out the type name
}
}
이 스 니펫을 실행하면 인쇄됩니다
System.Collections.Generic.List`1[System.Int32]
의 CLR 이름입니다 List<int>
. 런타임에 type 인수가입니다 System.Int32
. 이 만드는 바운드 개방형를.List<T>
런타임에 리플렉션을 사용하여 Type.MakeGenericType
메소드를 사용하여 유형 인수를 바인딩되지 않은 일반 유형의 지정되지 않은 유형 매개 변수에 바인딩 할 수 있습니다 .
Type unboundGenericList = typeof(List<>);
Type listOfInt = unboundGenericList.MakeGenericType(typeof(int));
if (listOfInt == typeof(List<int>))
Console.WriteLine("Constructed a List<int> type.");
유형이 특성을 사용하여 바인딩 된 유형을 구성 할 수 있는 바인딩되지 않은 일반 유형 ( 일반 유형 정의 ) 인지 여부를 확인할 수 있습니다 .Type.IsGenericTypeDefinition
Console.WriteLine(typeof(Dictionary<,>).IsGenericTypeDefinition); // True
Console.WriteLine(typeof(Dictionary<int,int>).IsGenericTypeDefinition); // False
런타임에 생성 된 형식에서 언 바운드 형식을 얻으려면 Type.GetGenericTypeDefinition
메서드를 사용할 수 있습니다 .
Type listOfInt = typeof(List<int>);
Type list = listOfInt.GetGenericTypeDefinition(); // == typeof(List<>)
제네릭 형식의 경우 완전히 바인딩되지 않은 형식 정의 또는 완전히 바인딩 된 정의를 가질 수 있습니다. 일부 유형 매개 변수를 바인딩하고 다른 유형 매개 변수를 바인딩 해제 할 수 없습니다. 예를 들어, 당신은 할 수 없습니다 Dictionary<int,>
나 Dictionary<,string>
.
추가하기 만하면됩니다.
Dictionary<string, T>
(또는 더 정확하게 Dictionary<string,>
는) 여전히 개방형입니다.
예:
void Foo<T>(Dictionary<string,T> dic) { ... }
An "open generic type" is just a generic type that doesn't yet have its type specified (e.g., CargoCrate<T>
). It becomes "closed" once a concrete type has been assigned (e.g. CargoCrate<Widget>
).
For example, say you have something like this:
public class Basket<T> {
T[] basketItems;
}
public class PicnicBlanket<T> {
Basket<T> picnicBasket; // Open type here. We don't know what T is.
}
// Closed type here: T is Food.
public class ParkPicnicBlanket : PicnicBlanket<Food> {
}
Here, picnicBasket
's type is open: nothing's yet been assigned to T
. When you make a concrete PicnicBlanket with a specific type -- for example, by writing PicnicBlanket<Food> p = new PicnicBlanket<Food>()
-- we now call it closed.
There are three kinds of generic types. To make it short, in this (simplified) declaration:
public class Dictionary<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>
Dictionary<TKey, TValue>
is an unbounded generic type.KeyValuePair<TKey, TValue>
is, in this case, an open constructed generic type. It has some type parameters, but they are already defined elsewhere (in Dictionary, in this case).Dictionary<string, int>
would be a closed constructed generic type.
참고URL : https://stackoverflow.com/questions/2173107/what-exactly-is-an-open-generic-type-in-net
'developer tip' 카테고리의 다른 글
Stateless (= Sessionless) 인증을 사용할 때 CSRF 토큰이 필요합니까? (0) | 2020.07.25 |
---|---|
XML 직렬화-배열의 루트 요소 렌더링 비활성화 (0) | 2020.07.25 |
다른 PHP 파일에서 한 PHP 파일의 함수를 호출하고 매개 변수를 전달하는 방법은 무엇입니까? (0) | 2020.07.25 |
java.io.File에 대한 java의 mkdir ()과 mkdirs ()의 차이점 (0) | 2020.07.25 |
iPad 사용자 에이전트 란 무엇입니까? (0) | 2020.07.25 |