【问题标题】:Reflection instead .NET-to-.NET scenario反射而不是 .NET 到 .NET 方案
【发布时间】:2021-10-13 15:22:52
【问题描述】:

我创建了 Person.dll 并在 Visual Studio 2019 的命令提示符中注册了它(regsvcs.exe)。注册后,我得到了 Person.tlb。我尝试在控制台项目中添加 Person.tlb 作为参考 COM 组件,但收到警告 MSB3290。

警告 MSB3290:无法为类型创建包装程序集 库“{8b1098cb-d453-4dc7-96ac-52df54d0a2ce}”。类型库 'Person' 是从 CLR 程序集中导出的,不能重新导入为 一个 CLR 程序集。

如何使用反射在控制台项目中添加 Person.tlb?

Person.dll:

using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
using System.Runtime.InteropServices;
using System.EnterpriseServices;

namespace COM
{
    [ClassInterface(ClassInterfaceType.None)]
    public class Person : ServicedComponent, COM.IPerson
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public bool IsMale { get; set; }
        public void Persist(string FilePath)
        {
            StreamWriter oFile = new StreamWriter(FilePath);
            XmlSerializer oXmlSerializer = new XmlSerializer(typeof(Person));
            oXmlSerializer.Serialize(oFile, this);
            oFile.Flush();
            oFile.Close();
        }
        static public Person Retrieve(string FilePath)
        {
            StreamReader oFile = new StreamReader(FilePath);
            XmlSerializer oXmlSerilizer = new XmlSerializer(typeof(Person));
            Person oPerson = oXmlSerilizer.Deserialize(oFile) as Person;
            return oPerson;

        }
    }
}

控制台项目:

using System;

namespace Test10
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            COM.Person per = new COM.Person();
            per.FirstName = "Maxim";
            per.LastName = "Donax";
            per.Persist(@" C:\myFile.xml ");
        }
    }
}

【问题讨论】:

  • 这通常发生在您尝试包装一个类型库时,该类型库是根据您正在使用的针对不同 CLR 版本编译的程序集创建的(例如 .NETFX 2.0 vs .NETFX 4.0 与 .NET Core)。

标签: c# .net dcom


【解决方案1】:

我使用了其他方式:在 Visual Studio 中创建 Person.dll 并注册它(regsvcs.exe)。在 Visual Basic 6.0 中使用参考 Person.tlb。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-15
    • 1970-01-01
    • 1970-01-01
    • 2010-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多