Khi bạn tìm thấy ở đó là một thiếu phương pháp ra khỏi hộp về những gì bạn cần, bạn luôn có thể viết phương pháp khuyến nông của riêng bạn.
public static Type GetEssenceType(this Type node) {
for(Type head=node, next; ; node=next)
if(null==(next=node.GetElementType()))
return node!=head?node:null;
}
Nó trả về nội nhất yếu tố loại (mà tôi gọi là loại chất) nếu loại nhất định (tên node
trong code) là một loại mà có yếu tố loại; nếu không, null
.
Edit:
Type
có một phương pháp nội bộ không điều tương tự:
internal virtual Type GetRootElementType()
{
Type elementType = this;
while (elementType.HasElementType)
{
elementType = elementType.GetElementType();
}
return elementType;
}
Bạn có thể tạo ra một đại biểu hoặc sử dụng nó thông qua phản ánh:
var bindingAttr=BindingFlags.Instance|BindingFlags.NonPublic;
var method=typeof(Type).GetMethod("GetRootElementType", bindingAttr);
var rootElementType=(Type)method.Invoke(givenType, null);
Lưu ý rằng GetRootElementType
trả về kiểu đã cho nếu nó không có kiểu phần tử.
Nguồn
2013-08-02 04:36:36
Điều gì đó dọc theo các dòng của var qq = new int [2,3] {{1, 2, 3}, {1, 2, 4}}; var bounds = Enumerable.Range (0, qq.Rank) .Chọn (r => qq.GetLowerBound (r)). ToArray(); var fst = qq.GetValue (giới hạn); nhưng lỗi của nó và phức tạp. –