首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试使用COM-interop从C#库追加vb6中的对象时,Get,“无法将‘字段’转换为‘字段’类型(相同的类)”

尝试使用COM-interop从C#库追加vb6中的对象时,Get,“无法将‘字段’转换为‘字段’类型(相同的类)”
EN

Stack Overflow用户
提问于 2021-03-11 05:39:18
回答 1查看 64关注 0票数 1

我有一个由遗留vb6代码使用的C#库。这对于大多数情况都很有效,除了我在追加对象时遇到了问题。

在这一部分中,我试图模拟一些access db函数调用,并将它们重定向到不同的db解决方案。但这与我遇到的问题并不是很相关,只是一些背景知识。

我有另一个对象和类,和这个非常相似;用一个Append也做了同样的事情。代码的唯一不同之处在于它不是一个使用的“对象”,而是一个固定的类型。我尝试过这样做,因为COM标准中不支持泛型。

任何想法或帮助都会非常有帮助。

在vb6中

代码语言:javascript
复制
Dim fld As Field
Set fld = new Field
NewTd.Fields.Append fld     'gives an error that Field can't be cast to type Field. 

C

字段:

代码语言:javascript
复制
 [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。

代码语言:javascript
复制
[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);
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-16 07:44:23

答案很简单。我更改了代码以期望在追加时出现"object“,并发现vb6错误代码更具体。它对代码的来源有问题。从那时起,我所做的就是创建项目(将其编译成可执行文件),一切都正常工作,没有例外,什么都没有。

通常看来,解决方案是在处理奇怪的错误时不依赖于vb6集成开发环境。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66573254

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档