RegAsm dll无已注册的类型。

6
我正在尝试使用RegAsm注册一个.dll文件。这是一个.NET 2.0的dll文件。所有类都是公共的,ComVisible为true。但我仍然得到RA0000:未注册任何类型的错误。下面是代码和assemblyinfo。如果有帮助,非常感谢!
STARTelnet.cs
/**
*Steven T. Norris     Created: 3/27/2012
*Last Updated By: Steven T. Norris     Last Updated On: 3/27/2012
*
*/

using System;
using MinimalisticTelnet;
using System.Net.Sockets;

/**
 * @brief Used to connect to, read, and respond to a STAR terminal session.
 * 
 * Steven T. Norris     Created: 3/27/2012
 */
namespace STARTelnet
{
    /**
     * Class used to connect to, read, and respond to a STAR terminal session. 
     */
    public class STARConnection
    {
        private TelnetConnection conn;
        private string output;
        private string command;
        private string prompt;

        /**
         * Instantiates new STARConnection. <br/>
         * Recommended login timeout is 2000. <br/>
         * Recommended overall timeout is 500. <br/>
         * Throws SocketException, PromptException, LoginException
         * 
         * @param [in] string username:Username for login
         * @param [in] string password:Password for login
         * @param [in] int loginTimeout:timeout milliseconds for login
         * @param [in] int overallTimeout:timeout milliseconds for session
         */
        public STARConnection(string username, string password, int loginTimeout, int overallTimeout)
        {
            output = "";
            conn = new TelnetConnection("HOSTHOSTHOST", 23);
            this.SetTimeout(overallTimeout);
            try
            {
                output = conn.Login(username, password, loginTimeout);
                if(output.Contains("You entered an invalid login name or password"))
                {
                    throw new LoginException("Failed to login");
                }
                this.ParsePrompt();
            }
            catch(Exception e)
            {
                if(e.Message.Contains("login prompt"))
                {
                    throw new PromptException("Login", "Could not find login prompt");
                }
                else if(e.Message.Contains("password prompt"))
                {
                    throw new PromptException("Password", "Could not find password prompt");
                }
                else
                {
                    throw e;
                }
            }
        }

        /**
         * Sets the timeout for the session in milliseconds
         * @param [in] int timeout:timeout for session
         */
        public void SetTimeout(int timeout)
        {
            conn.MainTimeOutMs = timeout;
            conn.TimeOutMs = timeout;
        }

        /**
         * Gets the current timeout for the session in milliseconds
         * @param [out] int:timout for session
         */
        public int GetTimeout()
        {
            return conn.TimeOutMs;
        }

        /**
         * Writes a command to the STAR session
         * @param [in] string command:command to write
         */
        public void Write(string command)
        {
            this.command = command;
            conn.Write(this.command);
            this.command = this.command.Replace("\n", "{newLine}");
        }


        /**
         * Writes a command followed by a new line (\n) to the STAR session
         * @param [in] string command:command to write
         */
        public void WriteLine(string command)
        {
            this.command = command;
            conn.WriteLine(this.command);
            this.command += "{newLine}";
        }

        /**
         * Reads output from STAR session. Assumes no data within given timeout denotes end of stream
         * @param [out] string:output from STAR session
         */
        public string Read()
        {
            output = conn.Read();
            this.ParsePrompt();
            return output;
        }

        /**
         * Reads output from STAR session with timeout changed for only this read. Assumes no data within
         * timeout denotes end of stream.
         * @param [in] int timeout:timeout for this read only
         * @param [out] string:output from STAR session
         */
        public string Read(int timeout)
        {
            int temp = this.GetTimeout();
            this.SetTimeout(timeout);
            this.Read();
            this.SetTimeout(temp);
            return output;
        }

        /*
         * Parse prompt from output
         */
        private void ParsePrompt()
        {
            prompt = output.Substring(output.LastIndexOf("\n") + 1);
        }

        /**
         * Gets output from last read
         * @param [out] string:output from last read
         */
        public string GetOutput()
        {
            return output;
        }

        /**
         * Gets last command entered
         * @param [out] string:last command entered
         */
        public string GetCommand()
        {
            return command;
        }

        /**
         * Gets prompt from last read
         * @param [out] string:last prompt
         */
        public string GetPrompt()
        {
            return prompt;
        }

        /**
         * Checks for connection
         * @param [out] bool:connection status
         */
        public bool IsConnected()
        {
            return conn.IsConnected;
        }
    }

    /**
     * Exception for failed logins
     */
    class LoginException: Exception
    {

        private string offender = "";
        public LoginException() : base() { }
        public LoginException(string message) : base(message) { }

        /**
         * Creates exception
         * @param string offender:element causing exception
         * @param string message:exception message
         */
        public LoginException(string offender, string message)
            : base(message)
        {
            this.offender = offender;
        }

        /**
         * To String method for getting exception string
         * @param [out] string:string representation of exception
         */
        public override string ToString()
        {
            if(offender == "")
            {
                return this.GetType() + ": "+this.Message+"\n"+this.StackTrace;
            }
            else
            {
                return "Incorrect login: " + offender + "--" + this.Message + "\n" + this.StackTrace;
            }
        }
    }

    /**
     * Exception for failed STAR prompts
     */
    class PromptException: Exception
    {

        private string prompt = "";
        public PromptException() : base(){ }
        public PromptException(string message) : base(message){ }

        /**
         * Creates exeption
         * @param string prompt:prompt causing exception
         * @param string message:exception message
         */
        public PromptException(string prompt, string message)
            : base(message)
        {
            this.prompt = prompt;
        }

        /**
         * To String method for getting exception string
         * @param [out] string:string representation of exception
         */
        public override string ToString()
        {
            if(prompt == "")
            {
                return this.GetType() + ": " + this.Message + "\n" + this.StackTrace;
            }
            else
            {
                return "Prompt failed: " + prompt + "--" + this.Message + "\n" + this.StackTrace;
            }
        }

    }
}

AssemblyInfo.cs

using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following 
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("STARTelnet")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("COMPANY")]
[assembly: AssemblyProduct("STARTelnet")]
[assembly: AssemblyCopyright("Copyright © COMPANY 2012")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible 
// to COM components.  If you need to access a type in this assembly from 
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(true)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("d7ae512d-c840-4ebc-8057-73a10f286225")]

// Version information for an assembly consists of the following four values:
//
//      Major Version
//      Minor Version 
//      Build Number
//      Revision
//
// You can specify all the values or you can default the Build and Revision Numbers 
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

尝试将您的汇编编译为 .NET 4.0。这是因为 regasm 如何注册和使用不同版本的 .NET 中的组件的某些复杂原因所需的。此外,尝试在类上添加 COMVisible(true) 属性,也许仅设置程序集的可见性不够。在注册时,您是否使用了 /codebase 开关? - Blablablaster
我必须编译为.NET 2.0以解决兼容性问题。我没有使用/codebase,但我注意到它之前被使用过。它的目的是什么? - steventnorris
当您不将程序集放入全局程序集缓存时,需要使用/codebase选项来使用其当前位置。如果您想要将程序集放入全局程序集缓存中,您需要为其分配强名称。 - Blablablaster
@Blablablaster 什么样的名称被认为是强名称?我使用的命名约定是否足够合格? - steventnorris
强名称是一个特殊的标记,可以在此处查看:http://msdn.microsoft.com/zh-cn/library/xc31ft41.aspx - Blablablaster
显示剩余2条评论
1个回答

13

我认为你的问题在于没有默认构造函数。请添加以下代码并调用。这是基于MSDN的指导

调用代码(当然在COM中看起来会不同)

var connection = new STARConnection();
connection.Initialize(username, password, loginTimeout, overallTimeout);

新代码

/// <summary>
/// Default constructor needed for COM. Set parameters with properties.
/// </summary>
public STARConnection()
{
}

public Initialize(string username, string password, int loginTimeout, int overallTimeout)
{
    output = "";
    conn = new TelnetConnection("HOSTHOSTHOST", 23);
    this.SetTimeout(overallTimeout);
    try
    {
        output = conn.Login(username, password, loginTimeout);
        if(output.Contains("You entered an invalid login name or password"))
        {
            throw new LoginException("Failed to login");
        }
        this.ParsePrompt();
    }
    catch(Exception e)
    {
        if(e.Message.Contains("login prompt"))
        {
            throw new PromptException("Login", "Could not find login prompt");
        }
        else if(e.Message.Contains("password prompt"))
        {
            throw new PromptException("Password", "Could not find password prompt");
        }
        else
        {
            throw e;
        }
    }
}

太好了!我添加了默认构造函数,而且没有出现任何问题。 - steventnorris
1
我删除了关于 ComVisible 属性的部分。你需要在类上使用它吗?根据我重新查阅的内容,微软表示只要公共的、非泛型类型具有默认构造函数并且程序集设置为 ComVisible(true),则这些类型都可被 COM 所见。 - Chris Benard
我所需要的只是默认构造函数。是的,所有的 ComVisible 都默认为 true(除非你使用的是 Visual Studio,在 assemblyinfo.cs 中会将所有的值显式设置为 false)。我还有第二个问题,但那是一个单独的问题,如果我找到解决方案,我会在几分钟内发布。 - steventnorris
我在这里提出了第二个有关另一个问题的问题。如果您能提供帮助,我将不胜感激。您似乎对COM和.NET dlls非常了解。该问题位于http://stackoverflow.com/questions/9977882/regasm-dll-net2-0-to-net4-0#comment12749230_9977882。 - steventnorris
@steventnorris,对于那个问题,我真的不知道。抱歉。:( - Chris Benard

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接