饼子天空  
个人程序Blogs

很久没有写C#代码了,今天与客户对接,客户是delphi写的程序,调用不了dll,希望我们把dll生成tlb文件模式,折腾了大半天总算解决了

第一步:vs工具创建个类库项目

第二步:查看下面代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

/**
 * 1:类上使用标签[ClassInterface(ClassInterfaceType.None)]
 * 2:方法要写成类实现接口模式
 * 3:项目-属性-生成--为COM互操作注册勾选上
 * 4:检查一下【Properties】中的AssemblyInfo.cs--[assembly: ComVisible(false)]设置为[assembly: ComVisible(true)]
 * 5:用管理员模式打开vs工具,就可以生成tlb文件了
 * **/
namespace TestTlb
{
    public interface ITestDemo
    {
        string fn1(string url);
        string fn2(string url);
    }

    /**这个标签一定要添加**/
    [ClassInterface(ClassInterfaceType.None)]
    public class Class1 : ITestDemo
    {
        public string fn1(string url)
        {
            try
            {
                ///写逻辑
                return "";  
            }
            catch (Exception ex)
            {
                return "";
            }
        }

        public string fn2(string url)
        {
            try
            {
                ///写逻辑
                return "";
            }
            catch (Exception ex)
            {
                return "";
            }
        }
    }
}

 

posted on 2022-10-28 18:28  饼子天空  阅读(371)  评论(0编辑  收藏  举报