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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:15:51+00:00 2026-06-10T04:15:51+00:00

I have a method in Class A public IList<T> MyMethod<T>() where T:AObject I want

  • 0

I have a method in Class A

public IList<T> MyMethod<T>() where T:AObject

I want to invoke this method in another generic class B. This T is without any constraints.

public mehtodInClassB(){
    if (typeof(AObject)==typeof(T))
    {
      //Compile error here, how can I cast the T to a AObject Type
      //Get the MyMethod data
        A a = new A();
        a.MyMethod<T>();
    }
}

Class C is inherited from Class AObject.

B<C> b = new B<C>();
b.mehtodInClassB() 

any thoughts?

After urs reminding…Update:

Yes. What I actually want to do is

typeof(AObject).IsAssignableFrom(typeof(T))

not

typeof(AObject)==typeof(T))
  • 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-10T04:15:52+00:00Added an answer on June 10, 2026 at 4:15 am

    If you know that T is an AObject, why not just provide AObject as the type parameter to MyMethod:

    if (typeof(AObject).IsAssignableFrom(typeof(T)))
    {
      //Compile error here, how can I cast the T to a AObject Type
      //Get the MyMethod data
        d.MyMethod<AObject>();
    }
    

    If providing AObject as a type parameter is not an option, you’ll have to put the same constraint on T in the calling method:

    void Caller<T>() where T: AObject
    {
        // ...
    
        d.MyMethod<T>();
    
        // ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a constructor method similar to this: public class Foo { public Foo
I have a proxied method in a MongoRepository extender class like this: public interface
I have two classes Order and Items I want a method like this class
I have a method which have this signature public static IList<T> GetBy<T>(System.Linq.Expressions.Expression<Func<T, bool>> expression)
Say i have a object like this public class Student{ public IList<Coursework> Courseworks{get;set;} public
So I have this method: public IList<IndicationProject> GetProjectsForSponsor(int sponsorId) { IList<IndicationProject> result = new
Say I have this method public void test(IList<AvaliableFeaturesVm> vm) { } I have this
I have a class with an attribute and getter method: public Class MyClass {
I have a method that prints private variable of the class. public class Test
I have the classes below: SuppliersRepository.cs (with the interface defining the method): public class

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.