Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kellabyte/2714766 to your computer and use it in GitHub Desktop.
Save kellabyte/2714766 to your computer and use it in GitHub Desktop.
public class TaskDataContractResolver : DataContractResolver
{
private Dictionary<string, Type> typesByNamespace = null;
private Dictionary<Type, string> typesByType = null;
public TaskDataContractResolver()
{
typesByNamespace = new Dictionary<string, Type>();
typesByType = new Dictionary<Type, string>();
ConstructTypeMap();
}
private void ConstructTypeMap()
{
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (Assembly assembly in assemblies)
{
var availableTypes = from t in assembly.GetTypes()
where t.GetCustomAttributes(
typeof(TaskValueAttribute),
true).Count() > 0
select t;
foreach (Type type in availableTypes)
{
object[] attributes = type.GetCustomAttributes(
typeof(TaskValueAttribute), true);
if (attributes.Length > 0)
{
TaskValueAttribute taskContent =
attributes[0] as TaskValueAttribute;
string ns = taskContent.TypeNamespace + type.Name;
typesByNamespace.Add(ns, type);
typesByType.Add(type, ns);
}
}
}
}
// Serialization
public override bool TryResolveType(Type type, Type declaredType,
DataContractResolver knownTypeResolver,
out System.Xml.XmlDictionaryString typeName,
out System.Xml.XmlDictionaryString typeNamespace)
{
Type[] genericTypes = type.GetGenericArguments();
string ns = string.Empty;
Type genericType = null;
foreach (Type genType in genericTypes)
{
if (typesByType.ContainsKey(genType) == true)
{
typesByType.TryGetValue(genType, out ns);
genericType = genType;
break;
}
}
if (string.IsNullOrEmpty(ns) == false)
{
XmlDictionary dictionary = new XmlDictionary();
typeName = dictionary.Add(genericType.Name);
typeNamespace = dictionary.Add(ns);
return true;
}
else
{
return knownTypeResolver.TryResolveType(
type, declaredType, null, out typeName, out typeNamespace);
}
}
// Deserialization
public override Type ResolveName(string typeName, string typeNamespace,
Type declaredType, DataContractResolver knownTypeResolver)
{
Type type = null;
typesByNamespace.TryGetValue(typeNamespace, out type);
if (type != null)
{
Type genType = typeof(ScheduledTask<>);
Type genericType = genType.MakeGenericType(type);
return genericType;
}
else
{
// Defer to the known type resolver
return knownTypeResolver.ResolveName(
typeName, typeNamespace, null, knownTypeResolver);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment