如何找到队列 程序集中的类型?

问题描述 投票:-3回答:1

我需要反序列化在不同计算机上创建的类。

程序非常相似,但是程序名称,.Net版本等有所不同。

下面的这段代码非常有效,除了我的班上有一个队列时。我无法搜索现有的程序集以找到合适的内容。

      public override Type BindToType(string assemblyName, string typeName)
        {
            string typeSearchString;// = typeName.Split('+')[1];

            try
            {
                typeSearchString = typeName.Split('+')[1];
            }
            catch
            {
                var parts = typeName.Split(new string[] { "[[" }, StringSplitOptions.RemoveEmptyEntries)[1].Split(',');
                typeSearchString = @"System.Collections.Generic.Queue"; //This does NOT find Queue<Double> but it does find something else which gives an error
            }

            Type tyType = null;
            string sShortAssemblyName = assemblyName.Split(',')[0];

            System.Reflection.Assembly[] ayAssemblies = AppDomain.CurrentDomain.GetAssemblies();

            foreach (System.Reflection.Assembly ayAssembly in ayAssemblies)
            {
                foreach (Type type in ayAssembly.GetTypes())
                {
                    if (type.FullName.Contains(typeSearchString))
                    {
                        tyType = ayAssembly.GetType(type.FullName);
                        return tyType; 
                    }
                }

            }
            return tyType;
        }

编辑1

这是我要从中进行搜索的条件:

typeName = "System.Collections.Generic.Queue`1[[System.Double, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]"
c# .net-assembly system.reflection
1个回答
0
投票

我自己的类似乎具有包含“ +”的typeName,例如:

“ MyNameSpace.MyDocName + MyClass”

我已经能够使用它来确定在哪里寻找类型:

      public override Type BindToType(string assemblyName, string typeName)
        {

            Type tyType = null;
            string sShortAssemblyName = assemblyName.Split(',')[0];

            System.Reflection.Assembly[] ayAssemblies = AppDomain.CurrentDomain.GetAssemblies();
            string typeSearchString;

            try
            {
                typeSearchString = typeName.Split('+')[1];

                foreach (System.Reflection.Assembly ayAssembly in ayAssemblies)
                {
                    foreach (Type type in ayAssembly.GetTypes())
                    {
                        if (type.FullName.Contains(typeSearchString))
                        {
                            tyType = ayAssembly.GetType(type.FullName);
                            return tyType;
                        }
                    }

                }
            }
            catch
            {
                foreach (System.Reflection.Assembly ayAssembly in ayAssemblies)
                {
                    if (sShortAssemblyName == ayAssembly.FullName.Split(',')[0])
                    {
                        tyType = ayAssembly.GetType(typeName);
                        return tyType;
                    }
                }
            }

            return tyType;
        }

[如果有人遇到类似问题,我会发布此答案。

© www.soinside.com 2019 - 2024. All rights reserved.