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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:59:22+00:00 2026-05-23T04:59:22+00:00

Imagine C# code like this ISomeInterface someVar = creator.Create(typeof(SomeClass), typeof(ISomeInterface)); What should happen? Method

  • 0

Imagine C# code like this

ISomeInterface someVar = creator.Create(typeof(SomeClass), 
                                        typeof(ISomeInterface));

What should happen? Method creator.Create takes two parameters: first is a type of an object to be created, second is a type that method should cast newly created object to and return as that type.

So, like in example code: we want use interface we know class implements so we ask for creation of object and specify that returned type should be that interface we are interested in.

Is is even possible to implement it somehow? Generics, reflection, dynamic method creation based on some complicated generics declaration? something more sophisticated? I’m open for suggestions.

Of course we are not taking into consideration simplest solution that is explicitly casting returned value to ISomeInterface. Method should return created object as interface we asked for.

If you wonder what do I need it for I’m trying to complicate my working code in futile pursuit of some clever way of simplifying my class factory 🙂

EDIT:

Current generic method Create:

public T Create<T>(Type type)
{
   return (T)Activator.CreateInstance(type);
}

But this is not what I want which is provide two types and basically create object of first but return as second. It is not a case that I have Create method for different interfaces implemented in my codebase.

Further example of usage:

  IFoo f = Create(typeof(Foo), typeof(IFoo));
  IBar b = Create(typeof(Bar), typeof(IBar));

So, as you can see method returns different interfaces each time according to type provided in second parameter. And there is no way to use type constrains or I don’t see how to do it.

  • 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-23T04:59:23+00:00Added an answer on May 23, 2026 at 4:59 am

    If you want your Create() method to return ISomeInterface without having to explicitly cast, then you obviously need to make it Create<ISomeInterface>().

    To dynamically construct and instance of passed type you obviously need to use reflection, though Activator would make it easier.

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

Sidebar

Related Questions

Why does everyone tell me writing code like this is a bad practice? if
I'm dealing with a lot of code that looks like this: if (btnLeftDock.BackColor !=
Imagine I have some code to read from the start to end of a
Imagine the following REBOL code: foo: context [bar: 3] I now have a context
Imagine I have a chunk of initialisation code at the top of a stored
Imagine the scene, you're updating some legacy Sybase code and come across a cursor.
Imagine this directory structure: app/ __init__.py sub1/ __init__.py mod1.py sub2/ __init__.py mod2.py I'm coding
I have a Javascript object like this: var count = { table:19, people:39, places_details:84,
Imagine you homebrew a custom gui framework that doesn't use windows handles (compact framework,
Imagine we have a program trying to write to a particular file, but failing.

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.