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

  • Home
  • SEARCH
  • 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 8985635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:22:18+00:00 2026-06-15T21:22:18+00:00

I have the following . public interface IMyService<T> where T: BaseModelType { Process(T input);

  • 0

I have the following .

public interface IMyService<T>
   where T: BaseModelType
{
    Process(T input);
}        

public class BaseModelType
{
  ...some property
}

public class SomeClass : BaseModelType
{
   ...some properties
}

public ServiceImpl : IMyService<SomeClass>
{
    ...the properties
}

Then I have a unity container where i register all the implementations of the generic interface. I want to be able to use the unitycontainer’s resolve method to get the interface, then do some work on it. At the time when i want to use the Resolve method i have the type in runtime

 new UnityContainer.Resolve(myTypeVar)

Can I somehow cast this to be

 IMyService<BaseModelType> value = new UnityContainer.Resolve(myTypeVar) //want to cast it here from object.

So that i can call the Process method that the interface defines.

  • 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-15T21:22:19+00:00Added an answer on June 15, 2026 at 9:22 pm

    No, because IMyService<SomeClass> does not implement IMyService<BaseModelType>. If you look at the implementation of the Process method:

    public void Process(SomeClass input){...}
    

    This clearly assumes that you’re giving it a SomeClass. It should be able to safely access any members of SomeClass. But if you called this method with a BaseModelType as the parameter, that wouldn’t work, would it?

    Assuming that you know at runtime that your input argument is going to be of the right type for the given generic IMyService<T> interface, you have two options:

    1. Invoke the generic method signature via reflection. A little slow, but effective.
    2. Add a non-generic parent interface for IMyService, which takes a BaseModelType. In your service implementations, you can implement this method by casting the input to the expected type for that implementation. This requires more code. But you could alleviate that somewhat by having a generic abstract base class that implements this method so the other implementations don’t have to.

      void Main()
      {
          var s = (IMyService)new ServiceImpl();
          s.Process(new SomeClass());
      }
      
      public interface IMyService
      {
          void Process(BaseModelType input);
      }
      
      public interface IMyService<in T> : IMyService
         where T: BaseModelType
      {
          void Process(T input);
      }
      
      public class BaseModelType{}
      
      public class SomeClass : BaseModelType{}
      
      public abstract class ServiceBase<T> : IMyService<T>
          where T: BaseModelType
      {
           void IMyService.Process(BaseModelType input)
           {
               Process((T)input);
           }
      
           public abstract void Process(T input);
      }
      
      public class ServiceImpl : ServiceBase<SomeClass>{
          public override void Process(SomeClass input){}
      }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following class definition: public interface IItem{} public class FirstObject<T, U> :
Let's say that I have the following interface: public interface IMyService { void SimpleMethod(int
I have following code public interface IEntity { // Properties int Id { get;
I have the following code: public interface IService { } public class MyService :
I have some code similar to the following: public interface IMyClass { MyEnum Value
I have the following: public interface ISubject { ... } public class Subject<T> :
I have the following classes public interface InterfaceBase { } public class ImplementA:InterfaceBase {
I have the following classes: public interface Emailer {} @Named public class RealEmailer implements
I have the following interface: public interface IPlateSubCategory<T> { T GetItem(int plateID); } And
I have the following interface: public interface SingleRecordInterface<T> { public void insert(T object); }

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.