C# RegAsm dll未注册任何类型
我正在尝试使用RegAsm注册.dll。它是一个.NET2.0DLL。所有类都是公共的,ComVisible是真实的。我仍然得到RA0000:没有注册类型错误。下面是代码和组装信息。任何帮助都会很好,谢谢 STARTelnet.csC# RegAsm dll未注册任何类型,c#,dll,regasm,C#,Dll,Regasm,我正在尝试使用RegAsm注册.dll。它是一个.NET2.0DLL。所有类都是公共的,ComVisible是真实的。我仍然得到RA0000:没有注册类型错误。下面是代码和组装信息。任何帮助都会很好,谢谢 STARTelnet.cs /** *Steven T. Norris Created: 3/27/2012 *Last Updated By: Steven T. Norris Last Updated On: 3/27/2012 * */ using System; us
/**
*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;
}
}
}
}
我认为你的问题是你没有默认的构造函数。请添加以下代码并调用。这是基于 调用代码(当然在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;
}
}
}
//
///COM所需的默认构造函数。使用属性设置参数。
///
公共STARConnection()
{
}
公共初始化(字符串用户名、字符串密码、int-loginTimeout、int-overallTimeout)
{
输出=”;
conn=新的TelnetConnection(“主机”,23);
此.SetTimeout(总超时);
尝试
{
输出=conn.Login(用户名、密码、loginTimeout);
if(output.Contains(“您输入了无效的登录名或密码”))
{
抛出新的LoginException(“登录失败”);
}
this.ParsePrompt();
}
捕获(例外e)
{
如果(e.Message.Contains(“登录提示”))
{
抛出新PrompException(“登录”,“找不到登录提示”);
}
else if(例如,Message.Contains(“密码提示”))
{
抛出新的PrompException(“密码”,“找不到密码提示”);
}
其他的
{
投掷e;
}
}
}
尝试将程序集编译为.NET 4.0。由于regasm如何注册和使用来自不同版本.NET的程序集的一些复杂原因,这是必需的。另外,请尝试使用COMVisible(true)属性装饰您的类,可能组件的comvisibility设置还不够。注册时是否使用/codebase开关?为了兼容性问题,我必须编译成.NET2.0。我没有使用/codebase,但我注意到以前使用过。它的用途是什么?/codebase在您不将程序集放入GAC时是必需的,因此它将从当前位置使用。如果您希望GAC中有一个程序集,则需要为其指定强名称。@Blablaster什么是强名称?我使用的命名约定够资格吗?强名称是一个特殊的标记,这里很完美!我添加了默认构造函数,它注册时没有问题。我删除了关于ComVisible属性的部分。你在课堂上是否需要这个?根据我重新查找的内容,Microsoft说所有公共的非泛型类型对COM都是可见的,只要它们有一个默认构造函数并且程序集是ComVisible(true)。我所需要的只是默认构造函数。是的,所有ComVisible都默认为true(当然,除非您使用的是VisualStudio,它在assemblyinfo.cs中将all显式设置为false)。虽然我还有第二个问题,但这是一个单独的问题,如果我能找到解决方案,我会在几分钟后发布。我在这里打开了关于另一个问题的第二个问题。如果你能提供帮助,我将不胜感激。您似乎了解COM和.NET DLL上的内容。它可以在@steventnorris找到,我真的不知道那一个。对不起(
/// <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;
}
}
}