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
|
//------------------------------------------------------------------------------
// <copyright file="Models.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">Microsoft</owner>
//------------------------------------------------------------------------------
namespace System.Xml.Serialization {
using System;
using System.Reflection;
using System.Collections;
using System.Diagnostics;
// These classes define the abstract serialization model, e.g. the rules for WHAT is serialized.
// The answer of HOW the values are serialized is answered by a particular reflection importer
// by looking for a particular set of custom attributes specific to the serialization format
// and building an appropriate set of accessors/mappings.
internal class ModelScope {
TypeScope typeScope;
Hashtable models = new Hashtable();
Hashtable arrayModels = new Hashtable();
internal ModelScope(TypeScope typeScope) {
this.typeScope = typeScope;
}
internal TypeScope TypeScope {
get { return typeScope; }
}
internal TypeModel GetTypeModel(Type type) {
return GetTypeModel(type, true);
}
internal TypeModel GetTypeModel(Type type, bool directReference) {
TypeModel model = (TypeModel)models[type];
if (model != null) return model;
TypeDesc typeDesc = typeScope.GetTypeDesc(type, null, directReference);
switch (typeDesc.Kind) {
case TypeKind.Enum:
model = new EnumModel(type, typeDesc, this);
break;
case TypeKind.Primitive:
model = new PrimitiveModel(type, typeDesc, this);
break;
case TypeKind.Array:
case TypeKind.Collection:
case TypeKind.Enumerable:
model = new ArrayModel(type, typeDesc, this);
break;
case TypeKind.Root:
case TypeKind.Class:
case TypeKind.Struct:
model = new StructModel(type, typeDesc, this);
break;
default:
if (!typeDesc.IsSpecial) throw new NotSupportedException(Res.GetString(Res.XmlUnsupportedTypeKind, type.FullName));
model = new SpecialModel(type, typeDesc, this);
break;
}
models.Add(type, model);
return model;
}
internal ArrayModel GetArrayModel(Type type) {
TypeModel model = (TypeModel)arrayModels[type];
if (model == null) {
model = GetTypeModel(type);
if (!(model is ArrayModel)) {
TypeDesc typeDesc = typeScope.GetArrayTypeDesc(type);
model = new ArrayModel(type, typeDesc, this);
}
arrayModels.Add(type, model);
}
return (ArrayModel)model;
}
}
internal abstract class TypeModel {
TypeDesc typeDesc;
Type type;
ModelScope scope;
protected TypeModel(Type type, TypeDesc typeDesc, ModelScope scope) {
this.scope = scope;
this.type = type;
this.typeDesc = typeDesc;
}
internal Type Type {
get { return type; }
}
internal ModelScope ModelScope {
get { return scope; }
}
internal TypeDesc TypeDesc {
get { return typeDesc; }
}
}
internal class ArrayModel : TypeModel {
internal ArrayModel(Type type, TypeDesc typeDesc, ModelScope scope) : base(type, typeDesc, scope) { }
internal TypeModel Element {
get { return ModelScope.GetTypeModel(TypeScope.GetArrayElementType(Type, null)); }
}
}
internal class PrimitiveModel : TypeModel {
internal PrimitiveModel(Type type, TypeDesc typeDesc, ModelScope scope) : base(type, typeDesc, scope) { }
}
internal class SpecialModel : TypeModel {
internal SpecialModel(Type type, TypeDesc typeDesc, ModelScope scope) : base(type, typeDesc, scope) { }
}
internal class StructModel : TypeModel {
internal StructModel(Type type, TypeDesc typeDesc, ModelScope scope) : base(type, typeDesc, scope) { }
internal MemberInfo[] GetMemberInfos() {
// we use to return Type.GetMembers() here, the members were returned in a different order: fields first, properties last
// Current System.Reflection code returns members in oposite order: properties first, then fields.
// This code make sure that returns members in the Everett order.
MemberInfo[] members = Type.GetMembers(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
MemberInfo[] fieldsAndProps = new MemberInfo[members.Length];
int cMember = 0;
// first copy all non-property members over
for (int i = 0; i < members.Length; i++) {
if ((members[i].MemberType & MemberTypes.Property) == 0) {
fieldsAndProps[cMember++] = members[i];
}
}
// now copy all property members over
for (int i = 0; i < members.Length; i++) {
if ((members[i].MemberType & MemberTypes.Property) != 0) {
fieldsAndProps[cMember++] = members[i];
}
}
return fieldsAndProps;
}
internal FieldModel GetFieldModel(MemberInfo memberInfo) {
FieldModel model = null;
if (memberInfo is FieldInfo)
model = GetFieldModel((FieldInfo)memberInfo);
else if (memberInfo is PropertyInfo)
model = GetPropertyModel((PropertyInfo)memberInfo);
if (model != null) {
if (model.ReadOnly && model.FieldTypeDesc.Kind != TypeKind.Collection && model.FieldTypeDesc.Kind != TypeKind.Enumerable)
return null;
}
return model;
}
void CheckSupportedMember(TypeDesc typeDesc, MemberInfo member, Type type) {
if (typeDesc == null)
return;
if (typeDesc.IsUnsupported) {
if (typeDesc.Exception == null) {
typeDesc.Exception = new NotSupportedException(Res.GetString(Res.XmlSerializerUnsupportedType, typeDesc.FullName));
}
throw new InvalidOperationException(Res.GetString(Res.XmlSerializerUnsupportedMember, member.DeclaringType.FullName + "." + member.Name, type.FullName), typeDesc.Exception);
}
CheckSupportedMember(typeDesc.BaseTypeDesc, member, type);
CheckSupportedMember(typeDesc.ArrayElementTypeDesc, member, type);
}
FieldModel GetFieldModel(FieldInfo fieldInfo) {
if (fieldInfo.IsStatic) return null;
if (fieldInfo.DeclaringType != Type) return null;
TypeDesc typeDesc = ModelScope.TypeScope.GetTypeDesc(fieldInfo.FieldType, fieldInfo, true, false);
if (fieldInfo.IsInitOnly && typeDesc.Kind != TypeKind.Collection && typeDesc.Kind != TypeKind.Enumerable)
return null;
CheckSupportedMember(typeDesc, fieldInfo, fieldInfo.FieldType);
return new FieldModel(fieldInfo, fieldInfo.FieldType, typeDesc);
}
FieldModel GetPropertyModel(PropertyInfo propertyInfo) {
if (propertyInfo.DeclaringType != Type) return null;
if (CheckPropertyRead(propertyInfo)) {
TypeDesc typeDesc = ModelScope.TypeScope.GetTypeDesc(propertyInfo.PropertyType, propertyInfo, true, false);
// Fix for CSDMain 100492, please contact arssrvlt if you need to change this line
if (!propertyInfo.CanWrite && typeDesc.Kind != TypeKind.Collection && typeDesc.Kind != TypeKind.Enumerable)
return null;
CheckSupportedMember(typeDesc, propertyInfo, propertyInfo.PropertyType);
return new FieldModel(propertyInfo, propertyInfo.PropertyType, typeDesc);
}
return null;
}
//CheckProperty
internal static bool CheckPropertyRead(PropertyInfo propertyInfo) {
if (!propertyInfo.CanRead) return false;
MethodInfo getMethod = propertyInfo.GetGetMethod();
if (getMethod.IsStatic) return false;
ParameterInfo[] parameters = getMethod.GetParameters();
if (parameters.Length > 0) return false;
return true;
}
}
internal enum SpecifiedAccessor {
None,
ReadOnly,
ReadWrite,
}
internal class FieldModel {
SpecifiedAccessor checkSpecified = SpecifiedAccessor.None;
MemberInfo memberInfo;
MemberInfo checkSpecifiedMemberInfo;
MethodInfo checkShouldPersistMethodInfo;
bool checkShouldPersist;
bool readOnly = false;
bool isProperty = false;
Type fieldType;
string name;
TypeDesc fieldTypeDesc;
internal FieldModel(string name, Type fieldType, TypeDesc fieldTypeDesc, bool checkSpecified, bool checkShouldPersist) :
this(name, fieldType, fieldTypeDesc, checkSpecified, checkShouldPersist, false) {
}
internal FieldModel(string name, Type fieldType, TypeDesc fieldTypeDesc, bool checkSpecified, bool checkShouldPersist, bool readOnly) {
this.fieldTypeDesc = fieldTypeDesc;
this.name = name;
this.fieldType = fieldType;
this.checkSpecified = checkSpecified ? SpecifiedAccessor.ReadWrite : SpecifiedAccessor.None;
this.checkShouldPersist = checkShouldPersist;
this.readOnly = readOnly;
}
internal FieldModel(MemberInfo memberInfo, Type fieldType, TypeDesc fieldTypeDesc) {
this.name = memberInfo.Name;
this.fieldType = fieldType;
this.fieldTypeDesc = fieldTypeDesc;
this.memberInfo = memberInfo;
this.checkShouldPersistMethodInfo = memberInfo.DeclaringType.GetMethod("ShouldSerialize" + memberInfo.Name, new Type[0]);
this.checkShouldPersist = this.checkShouldPersistMethodInfo != null;
FieldInfo specifiedField = memberInfo.DeclaringType.GetField(memberInfo.Name + "Specified");
if (specifiedField != null) {
if (specifiedField.FieldType != typeof(bool)) {
throw new InvalidOperationException(Res.GetString(Res.XmlInvalidSpecifiedType, specifiedField.Name, specifiedField.FieldType.FullName, typeof(bool).FullName));
}
this.checkSpecified = specifiedField.IsInitOnly ? SpecifiedAccessor.ReadOnly : SpecifiedAccessor.ReadWrite;
this.checkSpecifiedMemberInfo = specifiedField;
}
else {
PropertyInfo specifiedProperty = memberInfo.DeclaringType.GetProperty(memberInfo.Name + "Specified");
if (specifiedProperty != null) {
if (StructModel.CheckPropertyRead(specifiedProperty)) {
this.checkSpecified = specifiedProperty.CanWrite ? SpecifiedAccessor.ReadWrite : SpecifiedAccessor.ReadOnly;
this.checkSpecifiedMemberInfo = specifiedProperty;
}
if (this.checkSpecified != SpecifiedAccessor.None && specifiedProperty.PropertyType != typeof(bool)) {
throw new InvalidOperationException(Res.GetString(Res.XmlInvalidSpecifiedType, specifiedProperty.Name, specifiedProperty.PropertyType.FullName, typeof(bool).FullName));
}
}
}
if (memberInfo is PropertyInfo) {
readOnly = !((PropertyInfo)memberInfo).CanWrite;
isProperty = true;
}
else if (memberInfo is FieldInfo) {
readOnly = ((FieldInfo)memberInfo).IsInitOnly;
}
}
internal string Name {
get { return name; }
}
internal Type FieldType {
get { return fieldType; }
}
internal TypeDesc FieldTypeDesc {
get { return fieldTypeDesc; }
}
internal bool CheckShouldPersist {
get { return checkShouldPersist; }
}
internal SpecifiedAccessor CheckSpecified {
get { return checkSpecified; }
}
internal MemberInfo MemberInfo {
get { return memberInfo; }
}
internal MemberInfo CheckSpecifiedMemberInfo {
get { return checkSpecifiedMemberInfo; }
}
internal MethodInfo CheckShouldPersistMethodInfo {
get { return checkShouldPersistMethodInfo; }
}
internal bool ReadOnly {
get { return readOnly; }
}
internal bool IsProperty {
get { return isProperty; }
}
}
internal class ConstantModel {
FieldInfo fieldInfo;
long value;
internal ConstantModel(FieldInfo fieldInfo, long value) {
this.fieldInfo = fieldInfo;
this.value = value;
}
internal string Name {
get { return fieldInfo.Name; }
}
internal long Value {
get { return value; }
}
internal FieldInfo FieldInfo {
get { return fieldInfo; }
}
}
internal class EnumModel : TypeModel {
ConstantModel[] constants;
internal EnumModel(Type type, TypeDesc typeDesc, ModelScope scope) : base(type, typeDesc, scope) { }
internal ConstantModel[] Constants {
get {
if (constants == null) {
ArrayList list = new ArrayList();
FieldInfo[] fields = Type.GetFields();
for (int i = 0; i < fields.Length; i++) {
FieldInfo field = fields[i];
ConstantModel constant = GetConstantModel(field);
if (constant != null) list.Add(constant);
}
constants = (ConstantModel[])list.ToArray(typeof(ConstantModel));
}
return constants;
}
}
ConstantModel GetConstantModel(FieldInfo fieldInfo) {
if (fieldInfo.IsSpecialName) return null;
return new ConstantModel(fieldInfo, ((IConvertible)fieldInfo.GetValue(null)).ToInt64(null));
}
}
}
|