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
|
// <copyright>
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
namespace System.Activities.DynamicUpdate
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime;
using System.Runtime.Serialization;
using System.Text;
[DataContract]
internal class ArgumentInfo
{
// the following two fields are never to be serialized.
private Type type;
private bool HasGetTypeBeenAttempted;
string versionlessAssemblyQualifiedTypeName;
string name;
string fullAssemblyQualifiedTypeName;
ArgumentDirection direction;
public ArgumentInfo(RuntimeArgument argument)
{
this.Name = argument.Name;
Fx.Assert(argument.Type != null, "argument Type must not be null.");
this.Type = argument.Type;
this.HasGetTypeBeenAttempted = true;
this.FullAssemblyQualifiedTypeName = this.Type.AssemblyQualifiedName;
// this versionless assembly-qualified type name causes types of different versions
// to be considered equal for the sake of argument matching.
// Serializing the argument type info in a string format allows
// the map to be loaded into environment in which the types may not be available.
this.versionlessAssemblyQualifiedTypeName = GenerateVersionlessAssemblyQualifiedTypeName(argument.Type);
this.Direction = argument.Direction;
}
private Type Type
{
get
{
if (this.type == null && !this.HasGetTypeBeenAttempted)
{
// For every deserialized ArgumentInfo, we are here only the very first call to the property getter.
this.HasGetTypeBeenAttempted = true;
try
{
this.type = Type.GetType(this.FullAssemblyQualifiedTypeName, false);
}
catch (Exception e)
{
if (e is TypeLoadException || e is FileNotFoundException || e is FileLoadException || e is ArgumentException)
{
this.type = null;
FxTrace.Exception.AsWarning(e);
}
else
{
throw;
}
}
}
return this.type;
}
set
{
this.type = value;
}
}
public string Name
{
get
{
return this.name;
}
private set
{
this.name = value;
}
}
public string FullAssemblyQualifiedTypeName
{
get
{
return this.fullAssemblyQualifiedTypeName;
}
private set
{
this.fullAssemblyQualifiedTypeName = value;
}
}
public ArgumentDirection Direction
{
get
{
return this.direction;
}
private set
{
this.direction = value;
}
}
[DataMember(EmitDefaultValue = false, Name = "VersionlessAssemblyQualifiedTypeName")]
internal string SerializedVersionlessAssemblyQualifiedTypeName
{
get { return this.versionlessAssemblyQualifiedTypeName; }
set { this.versionlessAssemblyQualifiedTypeName = value; }
}
[DataMember(EmitDefaultValue = false, Name = "Name")]
internal string SerializedName
{
get { return this.Name; }
set { this.Name = value; }
}
[DataMember(EmitDefaultValue = false, Name = "FullAssemblyQualifiedTypeName")]
internal string SerializedFullAssemblyQualifiedTypeName
{
get { return this.FullAssemblyQualifiedTypeName; }
set { this.FullAssemblyQualifiedTypeName = value; }
}
[DataMember(EmitDefaultValue = false, Name = "Direction")]
internal ArgumentDirection SerializedDirection
{
get { return this.Direction; }
set { this.Direction = value; }
}
private static bool TypeEquals(ArgumentInfo left, ArgumentInfo right)
{
Fx.Assert(left != null && right != null, "both left and right must not be null.");
if (left.versionlessAssemblyQualifiedTypeName == right.versionlessAssemblyQualifiedTypeName)
{
return true;
}
//
// Try to determine if the two argument types are in fact the same due to one being a TypeForwardedTo type to the other.
// When forwarded types are used, it is expected that all the assemblies involved in type forwarding to be always available,
// whether during map calcuation, during implementation map rollup, or during merging of multiple maps.
//
if (left.Type != null && right.Type != null && left.Type == right.Type)
{
return true;
}
return false;
}
public static bool Equals(ArgumentInfo left, ArgumentInfo right)
{
if (left == null)
{
return right == null;
}
return right != null &&
left.Name == right.Name && TypeEquals(left, right) && left.Direction == right.Direction;
}
public static IList<ArgumentInfo> List(Activity activity)
{
if (activity.RuntimeArguments == null)
{
return new List<ArgumentInfo>();
}
return (from r in activity.RuntimeArguments select new ArgumentInfo(r)).ToList();
}
public override bool Equals(object obj)
{
ArgumentInfo operand = obj as ArgumentInfo;
return Equals(this, operand);
}
public override int GetHashCode()
{
return base.GetHashCode();
}
// generate assembly-qualified type name without assembly's version, culture and public token info
static string GenerateVersionlessAssemblyQualifiedTypeName(Type type)
{
StringBuilder sb = new StringBuilder();
BuildTypeSpec(sb, type);
return sb.ToString();
}
static void BuildTypeSpec(StringBuilder sb, Type type)
{
if (type.IsByRef)
{
BuildReferenceTypeSpec(sb, type);
}
else
{
BuildSimpleTypeSpec(sb, type);
}
}
static void BuildReferenceTypeSpec(StringBuilder sb, Type type)
{
Fx.Assert(type.HasElementType, "This type must have an element type.");
BuildSimpleTypeSpec(sb, type.GetElementType());
sb.Append('&');
}
static void BuildSimpleTypeSpec(StringBuilder sb, Type type)
{
if (type.IsPointer)
{
BuildPointerTypeSpec(sb, type);
}
else if (type.IsArray)
{
BuildArrayTypeSpec(sb, type);
}
else
{
BuildTypeName(sb, type);
}
}
static void BuildPointerTypeSpec(StringBuilder sb, Type type)
{
Fx.Assert(type.HasElementType, "This type must have an element type.");
BuildSimpleTypeSpec(sb, type.GetElementType());
sb.Append('*');
}
static void BuildArrayTypeSpec(StringBuilder sb, Type type)
{
Fx.Assert(type.IsArray, "This type must be an array type.");
Fx.Assert(type.HasElementType, "This type must have an element type.");
BuildSimpleTypeSpec(sb, type.GetElementType());
int arrayRank = type.GetArrayRank();
Fx.Assert(arrayRank > 0, "number of dimentions of this array must be greater than 0.");
sb.Append('[');
for (int i = 1; i < arrayRank; i++)
{
sb.Append(',');
}
sb.Append(']');
}
static void BuildTypeName(StringBuilder sb, Type type)
{
BuildNamespaceTypeName(sb, type);
sb.Append(", ");
BuildAssemblyNameSpec(sb, type);
}
static void BuildNamespaceTypeName(StringBuilder sb, Type type)
{
if (!String.IsNullOrEmpty(type.Namespace))
{
sb.Append(type.Namespace);
sb.Append('.');
}
BuildNestedTypeName(sb, type);
}
static void BuildNestedTypeName(StringBuilder sb, Type type)
{
if (type.IsNested)
{
BuildNestedTypeName(sb, type.DeclaringType);
sb.Append('+');
}
BuildSimpleName(sb, type);
}
static void BuildSimpleName(StringBuilder sb, Type type)
{
sb.Append(type.Name);
if (type.IsGenericType && !type.IsGenericTypeDefinition)
{
sb.Append('[');
Type[] genericArguments = type.GetGenericArguments();
sb.Append('[');
BuildTypeSpec(sb, genericArguments[0]);
sb.Append(']');
for (int i = 1; i < genericArguments.Length; i++)
{
sb.Append(',');
sb.Append('[');
BuildTypeSpec(sb, genericArguments[i]);
sb.Append(']');
}
sb.Append(']');
}
}
static void BuildAssemblyNameSpec(StringBuilder sb, Type type)
{
// only write assembly simple name,
// omit version, culture and public token
AssemblyName tempAssemName = new AssemblyName(type.Assembly.FullName);
sb.Append(tempAssemName.Name);
}
}
}
|