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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:25:11+00:00 2026-06-14T20:25:11+00:00

I was wondering if there is a way to pass in a type which

  • 0

I was wondering if there is a way to pass in a type which is enforced to be of a certain base type. For instance, for my game engine I have a attribute list (controllable, rigidBody, etc). I have a addAttribute parameter which takes a new object. However, I would like to also have an overload which takes a type instead from which I can create a new object on my own. So for instance

public void addAttribut(Type attribute)

I am thinking of maybe something that implements linq? I have tried but it looks like “where” can be only used on generics:

public void addAttribute(Type attribute) where attribute : Attribute

would be the signature of the function. I believe that Unity does this; however it could be that it can do this through Mono.

I could always throw an exception if it is not of the correct base type. However I was wondering if there is a way to prevent the programmer from passing in the wrong time all together.

Any ideas? Any help is greatly appreciated!

  • 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-14T20:25:12+00:00Added an answer on June 14, 2026 at 8:25 pm

    If you’re happy for calling code to need to know the type at compile-time, you could make it a generic method with a type parameter:

    public void AddAttribute<T>() where T : Attribute
    {
        // Use typeof(T)
    }
    

    Or to enforce a parameterless constructor which you can then call:

    public void AddAttribute<T>() where T : Attribute, new()
    {
        T t = new T();
        ...
    }
    

    But otherwise there’s no way of doing it, no – you’d have to validate the argument at execution time, just like any other.

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

Sidebar

Related Questions

I am wondering if there is any way to pass in a value when
I am wondering if there is a way to pass a variable to a
I'm wondering if there is a cleaner way to pass multiple variables in a
I was wondering if there is a way that I can pass variables like
I am just wondering, is there a way to pass the parameters to php
I have an interesting situation and I'm wondering if there is a better way
I am wondering if there is a way I can pass an updated PHP
I was wondering if there is any way to pass an array of objects
Just wondering is there any way I can check whether the url links to
I was wondering is there a way to give the user a small guided

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.