Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I’m trying to access a method from another assembly using Activator.CreateInstance method. This works fine in a test application but fails when I implement the same logic on the enterprise application.
The structure of the projects goes like this

C#
namespace SampleInterface // Class Library
{
    public interface IMethodExec
    {
        void TestMethod();
    }
}

namespace ProjectB // Class Library configured for COM+
{
    public class ClassB : IMethodExec
    {
        public void TestMethod()
        {
            //business logic goes here – uses external dll
        }

    }
}

namespace ProjectA // Windows application
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            IMethodExec exec = null;
            Type type = Type.GetTypeFromProgID("ProjectB.ClassB");
            exec = Activator.CreateInstance(type) as IMethodExec;
            exec.TestMethod();
        }
    }
}

namespace EntApp //Class Library configured for COM+
{
    public class Exec
    {
        IMethodExec exec = null;
        Type type = Type.GetTypeFromProgID("ProjectB.ClassB");
        exec = Activator.CreateInstance(type) as IMethodExec;
        exec.TestMethod();
    }
}


When I run ProjectA, it works correctly and Type type is
Name = "ClassB" FullName = " ProjectB.ClassB” at run-time

When I call ProjectB from EntApp, it fails at Activator.CreateInstance as Type type is Name = "__ComObject" FullName = "System.__ComObject" at run-time. EntApp is called from a client application. Any solution will be greatly appreciated. I’m open for alternative solution as long as the changes are in ProjectB and SampleInterface and method calling approach from EntApp.

What I have tried:

Tried referencing the ProjectB directly but the references in ProjectB were a conflict with the references in EntApp
Posted
Updated 16-Aug-21 21:01pm
v3
Comments
Richard Deeming 17-Aug-21 4:21am    
Removing the content of your question after it has been answered is extremely rude.

I have rolled back your destructive edit.
Sundar9057 17-Aug-21 12:00pm    
I'm sorry but the answer did not work. I was doing some more research and felt the question should have been simplified. No intentions of being rude or ignoring any solutions/answers. Thanks for taking the time to review and respond.

This could be the reason:
Quote:
This method is provided for COM support. ProgIDs are not used in the Microsoft .NET Framework because they have been superseded by the concept of namespace.

See: Type.GetTypeFromProgID Method (System) | Microsoft Docs[^]

And here is an article that might work for you:
Instantiating a C# object from a string using Activator.CreateInstance in .NET | Jeremy Lindsay[^]
 
Share this answer
 
v2
I'm able to get it to work with registry hack. Here is the link to the article Using a 32bit COM object in a 64bit environment[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900