Один объект COM работает, но другой не ⇐ C#

Место общения программистов C#
Anonymous
Один объект COM работает, но другой не

Сообщение Anonymous »

Имеет здесь очень странную проблему. Я пытаюсь сделать библиотеку .NET 8, которая будет вызвана из Excel VBA через Com Interop. < /P>
Я следил за этими двумя частями руководства:

expect .net Core Component .tlb файл, который Excel может ссылаться на. Мой второй тестовый класс должен был быть более сложным, но с помощью проб и ошибок я сделал второй класс более простым и проще, пока теперь не получу точный дублировать первого рабочего класса, но с другим именем. Один набор работает, другой - нет: < /p>

Код: Выделить всё

namespace MyTest.COM
{
[Guid("9d02f386-c90e-4353-964a-d3bdbad2c9f3")]
public class MyTestCOMDebug : IMyTestCOMDebug
{
public int Add(int x, int y) => x + y;
public string ToUpper(string s) => s.ToUpper();
}

[Guid("e6c5eb34-8412-4f17-a8e7-ef5e091841c5")]
public interface IMyTestCOMDebug
{
int Add(int x, int y);
string ToUpper(string s);
}

[Guid("f56b05d8-21aa-4ce8-8dd4-271eb102fea4")]
public class MyTestCOMDebug2 : IMyTestCOMDebug2
{
public int Add(int x, int y) => x + y;
public string ToUpper(string s) => s.ToUpper();
}

[Guid("23b13668-d7a3-4cfa-8a75-4a9063b2edb7")]
public interface IMyTestCOMDebug2
{
int Add(int x, int y);
string ToUpper(string s);
}
}
Итак, у меня есть mytestcomdebug: imytestcomdebug и mytestcomdebug2: imytestcomdebug2 . Те же участники, в том же файле того же проекта. < /P>
Это файл проекта: < /p>

Код: Выделить всё


net8.0-windows
enable
enable
true
true








all
runtime; build; native; contentfiles; analyzers; buildtransitive




true
MyTest


Я также имею файл assemblyinfo.cs со следующими атрибутами уровня сборки:

Код: Выделить всё

[assembly: ComVisible(true)]
[assembly: Guid("99134f79-a83c-4354-94f0-cfa52ea1ec0e")]
[assembly: ClassInterface(ClassInterfaceType.None)]
< /code>
Следующий код VBA показывает тест, который бросает ошибку: < /p>
Sub Test1()
Dim dbg As MyTest.IMyTestCOMDebug
Set dbg = CreateObject("MyTest.COM.MyTestCOMDebug")
Dim b As Integer
b = dbg.Add(2, 2)
MsgBox b
End Sub

Sub Test2()
Dim dbg As MyTest.IMyTestCOMDebug2
Set dbg = CreateObject("MyTest.COM.MyTestCOMDebug2")
Dim b As Integer
b = dbg.Add(2, 2)
MsgBox b
End Sub

< /code>
Test1
запускается без ошибок и правильно выводит 4 .
Test2< /code> дает следующую ошибку: < /p>

Ошибка времени выполнения '429': < /p>
Компонент Activex не может создать объект < /p>
< /blockquote>
Эта ошибка возникает в строке: < /p>
Set dbg = CreateObject("MyTest.COM.MyTestCOMDebug2")
< /code>
Now, why in the world does the first version of the class work, while the second one does not?
As you can see from the project file, I'm using registry-free COM, so there's no worry about having to unregister and re-register the library. I've made double sure that the file I'm testing with is the newly-built version. I've rebooted the computer. I'm testing with a completely new Excel file.
What am I missing here?

Подробнее здесь: https://stackoverflow.com/questions/796 ... r-does-not

Вернуться в «C#»