将自定义DataContractResolver与多个程序集一起使用

时间:2015-09-18 09:28:04

标签: c# xml xml-serialization .net-assembly datacontract

我在MEF应用程序中进行了以下设置:

汇编 MyBaseAssembly

namespace My.Namespace
{
    [DataContract]
    public class Container
    {
        [DataMember]
        public Data Item { get; set; }
    }

    [DataContract]
    public class Data
    {
        [DataMember]
        public string Foo { get; set; }
    }
}

汇编 SecondAssembly ,引用 MyBaseAssembly

namespace My.Another.Namespace
{
    [DataContract]
    public class SecondData : Data
    {
        [DataMember]
        public string Bar { get; set; }
    }
}

在我的应用程序内部的某处,我创建了一个Container对象:

Container container = new Container();
container.Item = new SecondData { Bar = "test" };

我想序列化和反序列化container对象。由于 SecondAssembly 是MEF模块,我需要动态检测和解析数据协定中的类型,因此KnownTypeAttribute不是一个好的解决方案。

我创建了一个自定义DataContractResolver,但我不知道如何获取反序列化的汇编信息。

在序列化时,我得到了以下XML:

<d4p1:SecondData
    xmlns:d6p1="http://schemas.datacontract.org/2004/07/My.Another.Namespace"
    i:type="d7p1:My.Another.Namespace.SecondData">
...
</d4p1:SecondData>

这是默认的DataContract序列化行为:我们获取类型名称和类型名称空间,但没有(显式)程序集信息!

尝试反序列化此XML,我无法确定用于解析类型的程序集:

class SerializationTypeResolver : DataContractResolver
{
    ...

    public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver)
    {
        Type result = knownTypeResolver.ResolveName(typeName, typeNamespace, declaredType, null);
        if (result == null)
        {
            // Here, I cannot rely on the declaredType parameter,
            // because it contains the declared type which is Data from MyBaseAssembly.
            // But I need the SecondData from the SecondAssembly!

            string assemblyName = ???; // How do I get this assembly name?
            string fullTypeName = typeName + ", " + assemblyName;
            result = Type.GetType(fullTypeName);
        }

        return result;
    }
}

所以我的问题是:在序列化和反序列化DataContract时,存储和获取程序集名称的好方法是什么?

3 个答案:

答案 0 :(得分:3)

为什么不在序列化时使用AssemblyQualifiedName?像这样:

internal class SerializationTypeResolver : DataContractResolver {
    public override bool TryResolveType(Type type, Type declaredType, DataContractResolver knownTypeResolver, out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace) {
        // not necessary to hardcode some type name of course, you can use some broader condition
        // like if type belongs to another assembly
        if (type.Name == "SecondData") {
            XmlDictionary dictionary = new XmlDictionary();
            // use assembly qualified name
            typeName = dictionary.Add(type.AssemblyQualifiedName);
            typeNamespace = dictionary.Add("http://tempuri.org"); // some namespace, does not really matter in this case
            return true;
        }
        return knownTypeResolver.TryResolveType(type, declaredType, null, out typeName, out typeNamespace);
    }

    public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver) {
        if (typeNamespace == "http://tempuri.org") {
            return Type.GetType(typeName); // assembly qualified already
        }
        return knownTypeResolver.ResolveName(typeName, typeNamespace, declaredType, null);
    }
}

答案 1 :(得分:1)

您需要遍历执行程序集的所有引用程序集(无论是否已加载),并查找可从declaredType分配的类型。答案Spinner Wrong color给出了一个起点。

class SerializationTypeResolver : DataContractResolver
{
    public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver)
    {
        Type result = knownTypeResolver.ResolveName(typeName, typeNamespace, declaredType, null);
        if (result == null)
        {
            foreach (var derivedType in declaredType.DerivedTypes())
            {
                XmlDictionaryString derivedTypeName;
                XmlDictionaryString derivedTypeNamespace;
                // Figure out if this derived type has the same data contract name and namespace as the incoming name & namespace.
                if (knownTypeResolver.TryResolveType(derivedType, derivedType, null, out derivedTypeName, out derivedTypeNamespace))
                {
                    if (derivedTypeName.Value == typeName && derivedTypeNamespace.Value == typeNamespace)
                    {
                        return derivedType;
                    }
                }
            }
        }

        return result;
    }
}

public static class TypeExtensions
{
    public static IEnumerable<Type> DerivedTypes(this Type baseType)
    {
        // TODO: Optimization: check if baseType is private or internal.
        var assemblies = baseType.Assembly.GetReferencingAssembliesAndSelf();
        Debug.Assert(assemblies.Count() == assemblies.Distinct().Count());
        return assemblies
            .SelectMany(a => a.GetTypes())
            .Where(t => baseType.IsAssignableFrom(t));
    }

    // Not sure which of the two versions of this method give better performance -- you might want to test yourself.

    public static IEnumerable<Type> DerivedTypesFromAllAssemblies(this Type baseType)
    {
        // TODO: Optimization: check if baseType is private or internal.
        var assemblies = AssemblyExtensions.GetAllAssemblies();
        Debug.Assert(assemblies.Count() == assemblies.Distinct().Count());
        return assemblies
            .SelectMany(a => a.GetTypes())
            .Where(t => baseType.IsAssignableFrom(t));
    }
}

public static class AssemblyExtensions
{
    public static IEnumerable<Assembly> GetAllAssemblies()
    {
        // Adapted from 
        // https://stackoverflow.com/questions/851248/c-sharp-reflection-get-all-active-assemblies-in-a-solution
        return Assembly.GetEntryAssembly().GetAllReferencedAssemblies();
    }

    public static IEnumerable<Assembly> GetAllReferencedAssemblies(this Assembly root)
    {
        // WARNING: Assembly.GetAllReferencedAssemblies() will optimize away any reference if there
        // is not an explicit use of a type in that assembly from the referring assembly --
        // And simply adding an attribute like [XmlInclude(typeof(T))] seems not to do
        // the trick.  See
        // https://social.msdn.microsoft.com/Forums/vstudio/en-US/17f89058-5780-48c5-a43a-dbb4edab43ed/getreferencedassemblies-not-returning-complete-list?forum=netfxbcl
        // Thus if you are using this to, say, discover all derived types of a base type, the assembly
        // of the derived types MUST contain at least one type that is referenced explicitly from the 
        // root assembly, directly or indirectly.

        var list = new HashSet<string>();
        var stack = new Stack<Assembly>();

        stack.Push(root);

        do
        {
            var asm = stack.Pop();

            yield return asm;

            foreach (var reference in asm.GetReferencedAssemblies())
                if (!list.Contains(reference.FullName))
                {
                    stack.Push(Assembly.Load(reference));
                    list.Add(reference.FullName);
                }

        }
        while (stack.Count > 0);
    }

    public static IEnumerable<Assembly> GetReferencingAssemblies(this Assembly target)
    {
        if (target == null)
            throw new ArgumentNullException();
        // Assemblies can have circular references:
        // https://stackoverflow.com/questions/1316518/how-did-microsoft-create-assemblies-that-have-circular-references
        // So a naive algorithm isn't going to work.

        var done = new HashSet<Assembly>();

        var root = Assembly.GetEntryAssembly();
        var allAssemblies = root.GetAllReferencedAssemblies().ToList();

        foreach (var assembly in GetAllAssemblies())
        {
            if (target == assembly)
                continue;
            if (done.Contains(assembly))
                continue;
            var refersTo = (assembly == root ? allAssemblies : assembly.GetAllReferencedAssemblies()).Contains(target);
            done.Add(assembly);
            if (refersTo)
                yield return assembly;
        }
    }

    public static IEnumerable<Assembly> GetReferencingAssembliesAndSelf(this Assembly target)
    {
        return new[] { target }.Concat(target.GetReferencingAssemblies());
    }
}

顺便说一句,您可以使用C# Reflection: Get all active assemblies in a solution?构造函数代替合约解析程序。

老实说,由于代码加载了根程序集引用的所有程序集,包括Microsoft DLL和第三方DLL,因此性能不太好。您可能希望通过在加载前检查DataContractSerializer(Type, IEnumerable<Type>)来开发一些减少要加载的程序集数量的方法,例如,如果基类来自您自己的name,则跳过Microsoft程序集。

答案 2 :(得分:1)

数百年前,我遇到了类似的情况 - 它不是MEF,而是我们手工制作的类似MEF的架构。 (MEF在他们的日子里有一个不好的代表。)我们对数据合同序列化器的创建进行了实际的集中治理,因此很容易插入代理提供者。

它本身不是合同解析器 - 但最终以类似的方式工作,并在与解析器相同的地点和时间插入到序列化管道中。

我从记忆中概括了这一点,这些年来很容易引起误解,但事情就这样了。我不记得的一个细节是AssemblyAwareSurrogate序列化了一个字节数组还是一个字符串。我猜想可以采取任何一种方式。

public class AssembyAwareSurrogateProvider: IDataContractSurrogate
{

  [DataContract]
  class AssemblyAwareSurrogate
  {
    [DataMember]
    public string AssemblyName { get; set; }
    [DataMember]
    public string TypeName { get; set; }

    [DataMember]
    public byte[ ] Object { get; set; }

    public AssemblyAwareSurrogate( object obj )
    {
      this.AssemblyName = obj.GetType( ).Assembly.FullName;
      this.TypeName = obj.GetType( ).FullName;

      var serializer = new DataContractSerializer( obj.GetType( ) );
      using ( var stream = new MemoryStream( ) )
      {
        serializer.WriteObject( stream, obj );
        stream.Flush( );
        Object = stream.ToArray( );
      }
    }
  }

  public Type GetDataContractType( Type type )
  {
    if ( SatisifesConstraints( type ) ) return typeof( AssemblyAwareSurrogate );
    return type;
  }
  private bool SatisifesConstraints( Type type )
  {
    //--> er - whatever types you're insterested in...
    return type != typeof( AssemblyAwareSurrogate );
  }

  public object GetDeserializedObject( object obj, Type targetType )
  {
    var surrogate = obj as AssemblyAwareSurrogate;
    if ( surrogate != null )
    {
      var assy = Assembly.Load( new AssemblyName( surrogate.AssemblyName ) );
      var serializer = new DataContractSerializer( assy.GetType( surrogate.TypeName ) );
      using ( var stream = new MemoryStream( surrogate.Object ) )
      {
        return serializer.ReadObject( stream );
      }
    }
    return obj;
  }

  public object GetObjectToSerialize( object obj, Type targetType )
  {
    if ( SatisifesConstraints( obj.GetType( ) ) )
    {
      return new AssemblyAwareSurrogate( obj );
    }
    return obj;
  }

  public object GetCustomDataToExport( Type clrType, Type dataContractType )
  {
    return null;
  }

  public object GetCustomDataToExport( MemberInfo memberInfo, Type dataContractType )
  {
    return null;
  }

  public void GetKnownCustomDataTypes( Collection<Type> customDataTypes )
  {
    throw new NotImplementedException( );
  }


  public Type GetReferencedTypeOnImport( string typeName, string typeNamespace, object customData )
  {
    throw new NotImplementedException( );
  }

  public CodeTypeDeclaration ProcessImportedType( CodeTypeDeclaration typeDeclaration, CodeCompileUnit compileUnit )
  {
    throw new NotImplementedException( );
  }
}