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 7923809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T17:26:08+00:00 2026-06-03T17:26:08+00:00

I am looking for a way to add an interface implementation to a class

  • 0

I am looking for a way to add an interface implementation to a class at runtime.
Here is a sample code, which I will discuss below.

Public Interface IAction
    Sub DoThis(a As Integer)
End Interface


Class Actor
Implements IAction

    Private mNumber As Integer

    Public Sub New(number As Integer)
        mNumber = number
    End Sub

    Public Sub DoThis(a As Integer) Implements IAction.DoThis
        Console.WriteLine(String.Format("Actor #{0}: {1}", mNumber, a))
    End Sub

End Class


Class ActionDispatcher
Implements IAction

    Private Shared mActionList As New List(Of IAction)

    Public Shared Sub Add(actor As IAction)
        mActionList.Add(actor)
    End Sub

    Public Sub DoThis(a As Integer) Implements IAction.DoThis
        For Each act In mActionList
            act.DoThis(a)
        Next
    End Sub

End Class


Module Module1

    Sub Main()
        Dim a As New ActionDispatcher
        ActionDispatcher.Add(New Actor(1))
        ActionDispatcher.Add(New Actor(2))
        a.DoThis(5)
    End Sub

End Module

This is related to WCF, where one needs to provide to CreateHost a single class, which implements all interfaces required for the end points. In this scenario, ActionDispatcher is such a class, and IAction is one of the many interfaces to be implemented.

My vision is to avoid implementing IAction in ActionDispatcher manually, but have some sort of registration mechanism, where I can say, that ActionDispatcher must implement interfaces IAction, IDoing, INoAction, etc – and have the dispatching methods generated for me.

MSDN says, that any class is compatible with any interface, so I don’t need to declare “Implements” in the ActionDispatcher. But I still need to implement the interface and connect the implementing method to the interface definition, so WCF can find it when needed.

The closest thing I found is probably the Automatic Interface Implementer, but it (1) creates a new type, (2) adds dummy methods.

I has also tried to understand CodeCompileUnit class, but so far couldn’t see the relation to what I need.

Can someone more experienced in Reflection API help me out? Is there a nice way to do whay I want?

  • 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-06-03T17:26:10+00:00Added an answer on June 3, 2026 at 5:26 pm

    I finally crunched it. For those interested in the intrications, the solution (in short) is:

    Class ActionDispatcher
    
        Private Shared mImplementorType As Type
    
        Public Shared Function GetImplementorType() As Type
            If mImplementorType Is Nothing
                mImplementorType = CreateImplementorType()
            End If
    
            Return mImplementorType
        End Function
    
        Private Shared Function CreateImplementorType() As Type
            ' Nice to have RunAndSave for debugging with ILdasm / ILSpy
            Dim myAssemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(
                New AssemblyName() With { .Name = "ViewerDispatcherAssembly" },
                AssemblyBuilderAccess.RunAndSave)
            Dim mbuilder = myAssemblyBuilder.DefineDynamicModule("ViewerDispatcherModule", "ViewerDispatcherModule.dll")
            Dim tbuilder = mbuilder.DefineType("ViewerDispatcherImpl", TypeAttributes.Class Or TypeAttributes.Public)
    
            For Each itype In mInterfaceTypes
                tbuilder.AddInterfaceImplementation(itype)
                For Each method In itype.GetMethods()
                    ' Create interface implementation for each interface method.
                    CreateInterfaceImplementation(tbuilder, itype, method, capability, mbuilder)
                Next
            Next
            Return tbuilder.CreateType()
        End Sub
    End Class
    

    The function CreateInterfaceImplementation dynamically creates a type to hold interface parameters and a method in this type to call the correct IAction function. It also creates the IAction implementation in tbuilder.
    There also exists an intermediate function to loop through mActionList to minimize the amount of the generated code.

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

Sidebar

Related Questions

I'm looking for a way to add an EventListener which will automatically removes itself
I'm looking for a way to add a value to price of configurable product
All, I am looking for a way to add an OCS presence icon the
I looking for a way to dynamicly add a filter to my statment without
Looking for a way to programatically, or otherwise, add a new instance of SQL
I've been looking into a way to add some duck typing to an F#
Looking for a way to add a couple of Simple XML objects together. The
im looking for a way to add some docblocks to a new .php file,
I am looking for a way to add an HTML element using JavaScript. But
I'm looking for a way to add auto complete functionality in Java a File

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.