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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:09:41+00:00 2026-05-31T23:09:41+00:00

Let’s imagine I have instantiated generic type by Activator.CreateInstance. Is there any way to

  • 0

Let’s imagine I have instantiated generic type by Activator.CreateInstance. Is there any way to cast it to ISomeInterface if I don’t have T in compile time (but I have it as Type instance)?

Here is a sample:

public static IPropertyAssertions<T> ShouldHave<T>(this T subject)
{
    var implementedInterfaces = subject.GetType().GetInterfaces();
    foreach (var interfaceType in implementedInterfaces)
    {
        if (!interfaceType.IsGenericType)
            continue;

        var genericType = interfaceType.GetGenericTypeDefinition();
        if (genericType == typeof(IEnumerable<>))
        {
            var genericTypeArg = genericType.GetGenericArguments()[0];

            var collectionPropertyAssertionsType = typeof (CollectionPropertyAssertions<>);

            Type makeme = collectionPropertyAssertionsType.MakeGenericType(genericTypeArg);

            return (IPropertyAssertions<ToDo: specify argument type here>) Activator.CreateInstance(makeme);
        }
    }

    ...
}

So I have extension method which can be called on IEnumerable<T>. In this case I would like to return not CollectionPropertyAssertions<IEnumerable<T>> but CollectionPropertyAssertions<T> where T is type of enumeration element.

  • 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-31T23:09:42+00:00Added an answer on May 31, 2026 at 11:09 pm

    You won’t be able to cast to it, no – you have to cast to a type which is known at compile-time… half the point is to then be able to use the specific members of that type.

    Some scenarios you may want to consider:

    • If you want to call a generic method which needs an ISomeInterface<T> as a parameter, you could call this via reflection or use dynamic typing
    • If you want to call members which don’t rely on T anyway, you might want to consider creating a non-generic base interface which ISomeInterface<T> extends – then you can just cast to the non-generic interface.

    If that doesn’t cover what you’re trying to do, please provide more details.

    EDIT: In this case you do know T because you’re in a generic method – so you only need to cast to IPropertyAssertions<T>.

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

Sidebar

Related Questions

Let's imagine I have a Java class of the type: public class MyClass {
Let's say I have this MySQL table: OK.. see the type field? Type 0
Let's say I have the following type type Key = String type Score =
Let's say i have this block of code, <div id=id1> This is some text
Let's say I have the following models class Photo(models.Model): tags = models.ManyToManyField(Tag) class Tag(models.Model):
Let's consider a simple grid, where any point is connected with at most 4
Let's say I have an image called hello.png with dimensions 200x100 . I create
Let's say for example i have URL containing the following percent encoded character :
Let's say I have two tables orgs and states orgs is (o_ID, state_abbr) and
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>

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.