How to serialize the delegate type property?

144 views Asked by At

I am trying to serialize a delegate into an xml file using a DataContractSerializer. It failed with an error

System.Runtime.Serialization.SerializationException : Type 'System.DelegateSerializationHolder+DelegateEntry' with data contract name 'DelegateSerializationHolder.DelegateEntry:http://schemas.datacontract.org/2004/07/System' is not expected. Consider using a DataContractResolver if you are using DataContractSerializer or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to the serializer.

So, I wrote the DataContractResolver something like below and it fails with an exception as System.ArgumentException : Invalid name character in '<>c'. The '<' character, hexadecimal value 0x3C, cannot be included in a name.

public override bool TryResolveType(Type type, Type declaredType, DataContractResolver knownTypeResolver,
            out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace)
        {
            var name = type.Name;
            var nameSpace = type.Namespace;

            typeName = new XmlDictionaryString(XmlDictionary.Empty, name, 0);
            if (nameSpace != null)
            {
                typeNamespace = new XmlDictionaryString(XmlDictionary.Empty, nameSpace, 0);

                if (!dictionary.ContainsKey(type.Name)) dictionary.Add(name, typeName);

                if (type.Namespace != null && !dictionary.ContainsKey(type.Namespace))
                    dictionary.Add(nameSpace, typeNamespace);

                return true;
            }

            typeName = null;
            typeNamespace = null;
            return false;
        }

What am I doing wrong? Please suggest. Any other way of serializing a delegate?

0

There are 0 answers