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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:13:41+00:00 2026-05-13T09:13:41+00:00

Can we restrict Type property of a class to be a specific type? Eg:

  • 0

Can we restrict Type property of a class to be a specific type?

Eg:

public interface IEntity { }

public class Entity : IEntity {}

public class NonEntity{}

class SampleControl {
    public Type EntityType{get;set;}
}

assume that sampleControl is UI class (may be Control,Form,..) its Value of EntityType Property should only accept the value of typeof(Entity), but not the the typeof(NonEntity) how can we restrict the user to give the specific type at Deign time(bcause – Sample is a control or form we can set its property at design time) , is this posible in C# .net

How can we achive this using C# 3.0?

In my above class I need the Type property that to this must be one of the IEntity.

  • 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-13T09:13:41+00:00Added an answer on May 13, 2026 at 9:13 am

    This might be a scenario where generics helps. Making the entire class generic is possible, but unfortunately the designer hates generics; don’t do this, but:

    class SampleControl<T> where T : IEntity { ... }
    

    Now SampleControl<Entity> works, and SampleControl<NonEntity> doesn’t.

    Likewise, if it wasn’t necessary at design time, you could have something like:

    public Type EntityType {get;private set;}
    public void SetEntityType<T>() where T : IEntity {
        EntityType = typeof(T);
    }
    

    But this won’t help with the designer. You’re probably going to have to just use validation:

    private Type entityType;
    public Type EntityType {
        get {return entityType;}
        set {
            if(!typeof(IEntity).IsAssignableFrom(value)) {
                throw new ArgumentException("EntityType must implement IEntity");
            }
            entityType = value;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We can restrict the type be the class or struct. Can we strict the
By default it can select all type of files,how to restrict it so that
In our example, we can choose to define an Enumerated Type that will restrict
I'm aware that Oracle does not have a boolean type to use for parameters,
I know that polymorphism can add a noticeable overhead. Calling a virtual function is
I'd like to say int x = magic(), y = moremagic(); return i =>
Let me explain my problem together with the background, so it would be easier
I've been banging my head against a wall trying to wrap my head around
I know some people may just respond never as long as there's user input.
When you pass a callback in some form to another function, you often have

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.