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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:27:07+00:00 2026-05-22T21:27:07+00:00

Possible Duplicate: Casting an object to two interfaces at the same time, to call

  • 0

Possible Duplicate:
Casting an object to two interfaces at the same time, to call a generic method

I’m fairly sure you can’t do this so I’m wondering if there’s a workaround, but I need/want to cast an object to represent multiple interfaces for use with generic constraints. For example:

public void Foo<T>(T t) where T : IInterfaceA, IInterfaceB
{
}

If I have an object I want to say something like var t = (IInterfaceA | IInterfaceB)someObj; so I can pass t into this method.

Is there a nifty way of doing this? I’m using C# 3.5 so no dynamic available, but if it’s possible with dynamic please post it anyway.

  • 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-22T21:27:08+00:00Added an answer on May 22, 2026 at 9:27 pm

    EDIT

    Despite the answer below, I would say the better solution is the one that most other answers point to. (This assumes that you can redefine the multiple classes that implement both interfaces.)

    Create an interface that inherits from both InterfaceA and InterfaceB, then, for all classes that implement interfaces A and B, replace those interfaces with the new one. Before:

    class SomeClass : IInterfaceA, IInterfaceB { }
    class AnotherClass : IInterfaceA, IInterfaceB { }
    class AdditionalClass : IInterfaceA, IInterfaceB { }
    

    After:

    interface IInterfaceC : IInterfaceA, IInterfaceB { }
    class SomeClass : IInterfaceC { }
    class AnotherClass : IInterfaceC { }
    class AdditionalClass : IInterfaceC { }
    

    The implementation of Foo is then fairly trivial. And, again, since you don’t know at compile time what type you have on hand, you may be able just to declare it as

    public void Foo(IInterfaceC someObj) { }
    

    END EDIT


    You can do it using reflection, though some will say that this isn’t particularly “nifty”:

    public class FooClass
    {
        public void Foo<T> (T t) where T : IInterfaceA, IInterfaceB
        {
            //... do your thing here
        }
        private static void Example(object someObj)
        {
            var type = someObj.GetType();
            if(typeof(IInterfaceA).IsAssignableFrom(type) && typeof(IInterfaceB).IsAssignableFrom(type))
            {
                var genericMethod = typeof(FooClass).GetMethod("Foo");
                var constructedMethod = genericMethod.MakeGenericMethod(type);
                var instance = new FooClass();
                var result = constructedMethod.Invoke(instance, new [] { someObj });
                Assert.IsNull(result);
            }
        }
    }
    

    you could also do this, which could allow you to make Foo non-generic. It’s also fairly ugly, so I would hide this ugliness by making it private:

    private void PrivateFoo(IInterfaceA objA, IInterfaceB objB)
    {
        if (!ReferenceEquals(objA, objB))
            throw new ArgumentException("objA and objB must refer to the same object");
    
        //... do your thing here
    }
    public void Foo(object someObj)
    {
        PrivateFoo((IInterfaceA)someObj, (IInterfaceB)someObj);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Casting vs using the ‘as’ keyword in the CLR Which method is
Possible Duplicate: casting vs using the ‘as’ keyword in the CLR hi, can somebody
Possible Duplicate: Direct casting vs 'as' operator? Can someone explain the difference to me
Possible Duplicate: Compile-time and runtime casting c# As I understand it, the following code
Possible Duplicate: Direct casting vs 'as' operator? Anyone can give a comparison between as
Possible Duplicate: Casting: (NewType) vs. Object as NewType Say for example I have a
Possible Duplicate: Best way to determine if two path reference to same file in
Possible Duplicate: Problem in calculating checksum : casting int to signed int32 This should
Possible Duplicates: Casting: (NewType) vs. Object as NewType Why is the C# “as” operator
Possible Duplicate: How do I calculate someone's age in C#? Maybe this could be

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.