Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6249471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:14:40+00:00 2026-05-24T13:14:40+00:00

I have a .net assembly that exposes a public class (named A) to be

  • 0

I have a .net assembly that exposes a public class (named A) to be created other programs using reflection (Assembly.CreateInstance). For now, it works fine.

But now I have two subclasses of A, say A1 and A2, and I need to create one or the other, depending on a runtime check. The client cannot be modified, it should believe that the object returned is of class A.

If I were using COM instead of .net, it would be easy enough: simply add the check into the IClassFactory.CreateInstance and be done. But is there a way to do this in .net?

Note: the obvious solution of using a proxy object, or similar, is not practical, because class A has several hundred methods (!) and I don’t want to reroute all of them.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T13:14:41+00:00Added an answer on May 24, 2026 at 1:14 pm

    Firstly, it should be noted that it would be easier if your client could use a factory method that you provide rather than having to hijack Activator.CreateInstance. Please consider that fully before reading on…

    Here’s an example using ContextBoundObject – it uses a counter as the “runtime check” to decide between B and C as the actual object, even though the caller asked for an A. Note that this also works if they used new A().

    private static void Main()
    {
        // subvert Activator.CreateInstance
        for(int i = 0 ; i < 100 ; i++)
        {
            object obj = Activator.CreateInstance(typeof (A));
            Console.WriteLine(obj.GetType().Name);
        }
        // subvert "new()"
        for(int i = 0 ; i < 100 ; i++)
        {
            object obj = new A();
            Console.WriteLine(obj.GetType().Name);
        }
    }
    
    class AProxyAttribute : ProxyAttribute
    {
        int flipper;
        public override MarshalByRefObject CreateInstance(Type serverType)
        {
            if (serverType == typeof(A))
            {
                return ((flipper++)%2) == 0 ? (A) new B() : (A) new C();
            }
            return base.CreateInstance(serverType);
        }
    }
    [AProxy]
    class A : ContextBoundObject { }
    // the two subclasses
    class B : A {}
    class C : A{}
    

    So yes, you can be dastardly and lie to the caller by giving them back something other than they asked for ;p This is fairly evil code, though. YMMV etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a VB.NET Class Library that exposes some COM Interop sub routines.
I have a .NET assembly DLL that is created on-the-fly from pre-compiled code in
I have .NET assembly with one public class and several private classes. I am
I have .NET assembly with ComVisible class. Some days ago (I can find that
I have been trying to access a .NET Assembly that I have created in
I have an assembly (.NET class library) that contains an object that implements EAP
I have an InteropUserControl in a .NET assembly that works fine in other operating
I have trouble to expose a .NET assembly in COM. It seems that I
I have a .NET assembly that contains classes to be registered as ServicedComponent through
I have a .net assembly that has a COM+ ServicedCopmonent in it and at

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.