1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
|
// This is a revised source code from CodeDom.
//------------------------------------------------------------------------------
// <copyright file="CodeTypeReference.cs" company="Microsoft">
//
// <OWNER>[....]</OWNER>
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace System.Runtime.Serialization {
using System.Diagnostics;
using System;
using Microsoft.Win32;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Globalization;
enum CodeTypeReferenceOptions {
GlobalReference = 0x00000001,
GenericTypeParameter = 0x00000002
}
class CodeTypeReference {
private string baseType;
[OptionalField]
private bool isInterface;
private int arrayRank;
private CodeTypeReference arrayElementType;
[OptionalField]
private List<CodeTypeReference> typeArguments;
[OptionalField]
private CodeTypeReferenceOptions referenceOptions;
[OptionalField]
private bool needsFixup = false;
public CodeTypeReference() {
baseType = string.Empty;
this.arrayRank = 0;
this.arrayElementType = null;
}
public CodeTypeReference(Type type) {
if (type == null)
throw new ArgumentNullException("type");
if (type.IsArray) {
this.arrayRank = type.GetArrayRank();
this.arrayElementType = new CodeTypeReference(type.GetElementType());
this.baseType = null;
} else {
InitializeFromType(type);
this.arrayRank = 0;
this.arrayElementType = null;
}
this.isInterface = type.IsInterface;
}
public CodeTypeReference (Type type, CodeTypeReferenceOptions codeTypeReferenceOption) : this(type) {
referenceOptions = codeTypeReferenceOption;
}
public CodeTypeReference (String typeName, CodeTypeReferenceOptions codeTypeReferenceOption) {
Initialize(typeName, codeTypeReferenceOption);
}
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
// We support the reflection format for generice type name.
// The format is like:
//
public CodeTypeReference(string typeName) {
Initialize(typeName);
}
private void InitializeFromType(Type type) {
baseType = type.Name;
if (!type.IsGenericParameter) {
Type currentType = type;
while (currentType.IsNested) {
currentType = currentType.DeclaringType;
baseType = currentType.Name + "+" + baseType;
}
if (!String.IsNullOrEmpty(type.Namespace))
baseType = type.Namespace + "." + baseType;
}
// pick up the type arguments from an instantiated generic type but not an open one
if (type.IsGenericType && !type.ContainsGenericParameters) {
Type[] genericArgs = type.GetGenericArguments();
for (int i = 0; i < genericArgs.Length; i++) {
TypeArguments.Add(new CodeTypeReference(genericArgs[i]));
}
}
else if (!type.IsGenericTypeDefinition)
{
// if the user handed us a non-generic type, but later
// appends generic type arguments, we'll pretend
// it's a generic type for their sake - this is good for
// them if they pass in System.Nullable class when they
// meant the System.Nullable<T> value type.
needsFixup = true;
}
}
private void Initialize(string typeName) {
Initialize(typeName, this.referenceOptions);
}
private void Initialize(string typeName, CodeTypeReferenceOptions options)
{
Options = options;
if (typeName == null || typeName.Length == 0) {
typeName = typeof(void).FullName;
this.baseType = typeName;
this.arrayRank = 0;
this.arrayElementType = null;
return;
}
typeName = RipOffAssemblyInformationFromTypeName(typeName);
int end = typeName.Length -1;
int current = end;
needsFixup = true; // default to true, and if we find arity or generic type args, we'll clear the flag.
// Scan the entire string for valid array tails and store ranks for array tails
// we found in a queue.
Queue q = new Queue();
while(current >= 0) {
int rank = 1;
if( typeName[current--] == ']') {
while(current >=0 && typeName[current] == ',') {
rank++;
current--;
}
if( current>=0 && typeName[current] == '[') { // found a valid array tail
q.Enqueue(rank);
current--;
end = current;
continue;
}
}
break;
}
// Try find generic type arguments
current = end;
ArrayList typeArgumentList = new ArrayList();
Stack subTypeNames = new Stack();
if( current > 0 && typeName[current--] == ']') {
needsFixup = false;
int unmatchedRightBrackets = 1;
int subTypeNameEndIndex = end;
// Try find the matching '[', if we can't find it, we will not try to parse the string
while(current >= 0) {
if( typeName[current] == '[' ) {
// break if we found matched brackets
if( --unmatchedRightBrackets == 0) break;
}
else if( typeName[current] == ']' ) {
++unmatchedRightBrackets;
}
else if( typeName[current] == ',' && unmatchedRightBrackets == 1) {
//
// Type name can contain nested generic types. Following is an example:
// System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],
// [System.Collections.Generic.List`1[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]],
// mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
//
// Spliltting by ',' won't work. We need to do first-level split by ','.
//
if( current + 1 < subTypeNameEndIndex) {
subTypeNames.Push(typeName.Substring(current+1 , subTypeNameEndIndex - current - 1));
}
subTypeNameEndIndex = current;
}
--current;
}
if( current > 0 && (end - current - 1) > 0) {
// push the last generic type argument name if there is any
if( current + 1 < subTypeNameEndIndex) {
subTypeNames.Push(typeName.Substring(current+1 , subTypeNameEndIndex - current - 1));
}
// we found matched brackets and the brackets contains some characters.
while( subTypeNames.Count > 0) {
String name = RipOffAssemblyInformationFromTypeName((string)subTypeNames.Pop());
typeArgumentList.Add(new CodeTypeReference(name));
}
end = current - 1;
}
}
if( end < 0) { // this can happen if we have some string like "[...]"
this.baseType = typeName;
return;
}
if (q.Count > 0 ) {
CodeTypeReference type = new CodeTypeReference(typeName.Substring(0, end + 1), Options);
for(int i = 0; i < typeArgumentList.Count; i++) {
type.TypeArguments.Add((CodeTypeReference)typeArgumentList[i]);
}
while( q.Count > 1) {
type = new CodeTypeReference( type, (int)q.Dequeue());
}
// we don't need to create a new CodeTypeReference for the last one.
Debug.Assert(q.Count == 1 , "We should have one and only one in the rank queue.");
this.baseType = null;
this.arrayRank = (int)q.Dequeue();
this.arrayElementType = type;
}
else if( typeArgumentList.Count > 0 ) {
for( int i = 0; i < typeArgumentList.Count; i++) {
TypeArguments.Add((CodeTypeReference)typeArgumentList[i]);
}
this.baseType = typeName.Substring(0, end + 1);
}
else{
this.baseType = typeName;
}
// Now see if we have some arity. baseType could be null if this is an array type.
if (baseType != null && baseType.IndexOf('`') != -1)
needsFixup = false;
}
public CodeTypeReference(string typeName, params CodeTypeReference[] typeArguments) : this(typeName){
if( typeArguments != null && typeArguments.Length > 0) {
TypeArguments.AddRange(typeArguments);
}
}
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public CodeTypeReference(string baseType, int rank) {
this.baseType = null;
this.arrayRank = rank;
this.arrayElementType = new CodeTypeReference(baseType);
}
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public CodeTypeReference(CodeTypeReference arrayType, int rank) {
this.baseType = null;
this.arrayRank = rank;
this.arrayElementType = arrayType;
}
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public CodeTypeReference ArrayElementType {
get {
return arrayElementType;
}
set {
arrayElementType = value;
}
}
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public int ArrayRank {
get {
return arrayRank;
}
set {
arrayRank = value;
}
}
internal int NestedArrayDepth {
get {
if (arrayElementType == null)
return 0;
return 1 + arrayElementType.NestedArrayDepth;
}
}
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public string BaseType {
get {
if (arrayRank > 0 && arrayElementType != null) {
return arrayElementType.BaseType;
}
if (String.IsNullOrEmpty(baseType))
return string.Empty;
string returnType = baseType;
if (needsFixup && TypeArguments.Count > 0)
returnType = returnType + '`' + TypeArguments.Count.ToString(CultureInfo.InvariantCulture);
return returnType;
}
set {
baseType = value;
Initialize(baseType);
}
}
[System.Runtime.InteropServices.ComVisible(false)]
public CodeTypeReferenceOptions Options {
get { return referenceOptions;}
set { referenceOptions = value;}
}
[System.Runtime.InteropServices.ComVisible(false)]
public List<CodeTypeReference> TypeArguments{
get {
if (arrayRank > 0 && arrayElementType != null) {
return arrayElementType.TypeArguments;
}
if( typeArguments == null) {
typeArguments = new List<CodeTypeReference>();
}
return typeArguments;
}
}
internal bool IsInterface {
get {
// Note that this only works correctly if the Type ctor was used. Otherwise, it's always false.
return this.isInterface;
}
}
//
// The string for generic type argument might contain assembly information and square bracket pair.
// There might be leading spaces in front the type name.
// Following function will rip off assembly information and brackets
// Following is an example:
// " [System.Collections.Generic.List[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral,
// PublicKeyToken=b77a5c561934e089]], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]"
//
private string RipOffAssemblyInformationFromTypeName(string typeName) {
int start = 0;
int end = typeName.Length - 1;
string result = typeName;
// skip white space in the beginning
while( start < typeName.Length && Char.IsWhiteSpace(typeName[start])) start++;
while( end >= 0 && Char.IsWhiteSpace(typeName[end])) end--;
if(start < end) {
if (typeName[start] =='[' && typeName[end] == ']') {
start++;
end--;
}
// if we still have a ] at the end, there's no assembly info.
if (typeName[end] != ']') {
int commaCount = 0;
for(int index = end; index >= start; index--) {
if( typeName[index] == ',') {
commaCount++;
if( commaCount == 4) {
result = typeName.Substring( start, index - start);
break;
}
}
}
}
}
return result;
}
}
}
|