Files
mono/mcs/class/System.XML/System.Xml.Serialization/ReflectionHelper.cs
T
Gert Driesen 34560e9e64 * CodeExporter.cs: Set eol-style to native.
* CodeGenerationOptions.cs: Same.
* CodeIdentifier.cs: Same.
* ImportContext.cs: Same.
* IXmlSerializable.cs: Same.
* IXmlSerializerImplementation.cs: Same.
* IXmlTextParser.cs: Same.
* MapCodeGenerator.cs: Same.
* ReflectionHelper.cs: Same. Fixed line endings.
* SchemaImporter.cs: Same.
* SchemaImporterExtensionCollection.cs: Same.
* SchemaImporterExtension.cs: Same.
* SchemaTypes.cs: Same.
* SerializationCodeGeneratorConfiguration.cs: Same.
* SerializationCodeGenerator.cs: Same.
* SerializationSource.cs: Same.
* SoapReflectionImporter.cs: Same.
* SoapSchemaExporter.cs: Same.
* TypeData.cs: Same.
* TypeTranslator.cs: Same.
* UnreferencedObjectEventHandler.cs: Same.
* XmlAnyElementAttribute.cs: Same.
* XmlArrayItemAttribute.cs: Same.
* XmlAttributeEventHandler.cs: Same.
* XmlAttributeOverrides.cs: Same.
* XmlAttributes.cs: Same.
* XmlDeserializationEvents.cs: Same.
* XmlElementEventHandler.cs: Same.
* XmlIncludeAttribute.cs: Same.
* XmlMapping.cs: Same.
* XmlMemberMapping.cs: Same.
* XmlMembersMapping.cs: Same.
* XmlNodeEventHandler.cs: Same.
* XmlReflectionImporter.cs: Same.
* XmlSchemaExporter.cs: Same.
* XmlSchemaProviderAttribute.cs: Same.
* XmlSerializationCollectionFixupCallback.cs: Same.
* XmlSerialiationFixupCallback.cs: Same.
* XmlSerializationGeneratedCode.cs: Same.
* XmlSerializationReadCallback.cs: Same.
* XmlSerializationReader.cs: Same.
* XmlSerializationReaderInterpreter.cs: Same.
* XmlSerializationWriteCallback.cs: Same.
* XmlSerializationWriterInterpreter.cs: Same.
* XmlSerializerAssemblyAttribute.cs: Same.
* XmlSerializer.cs: Same.
* XmlSerializerFactory.cs: Same.
* XmlSerializerVersionAttribute.cs: Same.
* XmlTypeMapElementInfo.cs: Same.
* XmlTypeMapMemberAttribute.cs: Same.
* XmlTypeMapMember.cs: Same.
* XmlTypeMapMemberNamespaces.cs: Same.


svn path=/trunk/mcs/; revision=56798
2006-02-11 12:52:46 +00:00

120 lines
3.8 KiB
C#

//
// System.Xml.Serialization.ReflectionHelper
//
// Author:
// Lluis Sanchez Gual ([email protected])
//
// Copyright (C) 2003 Ximian, Inc.
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Reflection;
using System.Collections;
namespace System.Xml.Serialization
{
internal class ReflectionHelper
{
Hashtable _clrTypes = new Hashtable ();
Hashtable _schemaTypes = new Hashtable ();
public void RegisterSchemaType (XmlTypeMapping map, string xmlType, string ns)
{
string mapKey = xmlType + "/" + ns;
if (!_schemaTypes.ContainsKey (mapKey))
_schemaTypes.Add (mapKey, map);
}
public XmlTypeMapping GetRegisteredSchemaType (string xmlType, string ns)
{
string mapKey = xmlType + "/" + ns;
return _schemaTypes[mapKey] as XmlTypeMapping;
}
public void RegisterClrType (XmlTypeMapping map, Type type, string ns)
{
if (type == typeof(object)) ns = "";
string mapKey = type.FullName + "/" + ns;
if (!_clrTypes.ContainsKey (mapKey))
_clrTypes.Add (mapKey, map);
}
public XmlTypeMapping GetRegisteredClrType (Type type, string ns)
{
if (type == typeof(object)) ns = "";
string mapKey = type.FullName + "/" + ns;
return _clrTypes[mapKey] as XmlTypeMapping;
}
public Exception CreateError (XmlTypeMapping map, string message)
{
return new InvalidOperationException ("There was an error reflecting '" + map.TypeFullName + "': " + message);
}
public static void CheckSerializableType (Type type, bool allowPrivateConstructors)
{
if (type.IsArray) return;
if (!allowPrivateConstructors && type.GetConstructor (Type.EmptyTypes) == null && !type.IsAbstract && !type.IsValueType)
throw new InvalidOperationException (type.FullName + " cannot be serialized because it does not have a default public constructor");
if (type.IsInterface && !TypeTranslator.GetTypeData (type).IsListType)
throw new InvalidOperationException (type.FullName + " cannot be serialized because it is an interface");
Type t = type;
Type oldt = null;
do {
if (!t.IsPublic && !t.IsNestedPublic)
throw new InvalidOperationException (type.FullName + " is inaccessible due to its protection level. Only public types can be processed");
oldt = t;
t = t.DeclaringType;
}
while (t != null && t != oldt);
}
public static string BuildMapKey (Type type)
{
return type.FullName + "::";
}
public static string BuildMapKey (MethodInfo method, string tag)
{
string res = method.DeclaringType.FullName + ":" + method.ReturnType.FullName + " " + method.Name + "(";
ParameterInfo[] pars = method.GetParameters ();
for (int n=0; n<pars.Length; n++)
{
if (n > 0) res += ", ";
res += pars[n].ParameterType.FullName;
}
res += ")";
if (tag != null)
res += ":" + tag;
return res;
}
}
}