我有一个遗留vb6代码使用的c#库。这在大多数情况下都能很好地工作,除了我遇到附加对象的问题。
在部分中,我试图模拟一些访问db函数调用,并将它们重定向到不同的db解决方案。但这与我遇到的问题无关,只是一些背景知识。我有另一个与此非常相似的对象和类;对Append也做同样的事情。代码的唯一不同之处在于它不是一个"对象"。这是一种固定的类型。我试过这样做,因为COM标准中不支持泛型。
任何想法或帮助都会很有帮助。
在vb6Dim fld As Field
Set fld = new Field
NewTd.Fields.Append fld 'gives an error that Field can't be cast to type Field.
c#:
[Guid("2515418d-04af-484e-bb3b-fe53a6121f73")]
[ClassInterface(ClassInterfaceType.None)]
public class Field : IField
{
public string Name { get; set; }
public string Value { get; set; }
public int Type { get; set; } //private int TypePV;
// 3 more public ints.
enum FieldType
{
// different field types.
}
---
[Guid("fd362bff-da4e-4419-a379-1ed84ff74f1b")]
public interface IField
{
string Name { get; set; }
string Value { get; set; }
int Type { get; set; }
// three more ints.
}
其中定义了Append
[Guid("0cf3c33f-8ca8-4a5b-8382-d1612558fcad")]
[ClassInterface(ClassInterfaceType.None)]
public class FieldsO : IFieldsO
{
public object obj;
public FieldsO(object cobj)
{
obj = cobj;
}
public object this[string param]
{
get
{
if (obj.GetType() == typeof(Recordset))
return ((Recordset)obj)[param];
if (obj.GetType() == typeof(TableDef))
return ((TableDef)obj)[param];
return null;
}
set
{
if (obj.GetType() == typeof(Recordset))
((Recordset)obj)[param] = value;
if (obj.GetType() == typeof(TableDef))
((TableDef)obj)[param] = (IField)value;
}
}
public void Append(Field io) {
if (obj.GetType() == typeof(TableDef)) {
if (!((TableDef)obj)._Fields.ContainsKey(io.Name))
((TableDef)obj)._Fields.Add(io.Name, io);
}
}
public int Count() {
if (obj.GetType() == typeof(Recordset))
return ((Recordset)obj).dt.Columns.Count;
if (obj.GetType() == typeof(TableDef))
return ((TableDef)obj)._Fields.Count;
return -1;
}
}
---
[Guid("fe528160-1505-448e-bb9c-8ccfd9e3643d")]
public interface IFieldsO
{
object this[string param] { get; set; }
int Count();
void Append(Field io);
}
答案很简单。我修改了代码,期望得到一个"对象"。在Append上,发现vb6的错误代码更具体。它在代码的来源上有问题。从那里,我所做的就是制作项目(将其编译成可执行文件),一切都正常工作,没有异常,什么也没有。
解决方案,就像它经常看起来的那样,在处理奇怪的错误时不依赖于vb6 IDE。