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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T14:31:02+00:00 2026-05-10T14:31:02+00:00

Is there a way to enforce/limit the types that are passed to primitives? (bool,

  • 0

Is there a way to enforce/limit the types that are passed to primitives? (bool, int, string, etc.)

Now, I know you can limit the generic type parameter to a type or interface implementation via the where clause. However, this doesn’t fit the bill for primitives (AFAIK) because they do not all have a common ground (apart from object before someone says! :P).

So, my current thoughts are to just grit my teeth and do a big switch statement and throw an ArgumentException on failure.


EDIT 1:

Just to clarify:

The code definition should be like this:

public class MyClass<GenericType> .... 

And instantiation:

MyClass<bool> = new MyClass<bool>(); // Legal MyClass<string> = new MyClass<string>(); // Legal MyClass<DataSet> = new MyClass<DataSet>(); // Illegal MyClass<RobsFunkyHat> = new MyClass<RobsFunkyHat>(); // Illegal (but looks awesome!) 

EDIT 2

@Jon Limjap – Good point, and something I was already considering. I’m sure there is a generic method that can be used to determine if the type is of a value or reference type.

This could be useful in instantly removing a lot of the objects I don’t want to deal with (but then you need to worry about the structs that are used such as Size ). Interesting problem no? 🙂

Here it is:

where T: struct 

Taken from MSDN.


I’m curious. Could this be done in .NET 3.x using extension methods? Create an interface, and implement the interface in the extension methods (which would probably be cleaner than a bit fat switch). Plus if you then need to later extend to any lightweight custom types, they can also implement the same interface, with no changes required to the base code.

What do you guys think?

The sad news is I am working in Framework 2!! 😀


EDIT 3

This was so simple following on from Jon Limjaps Pointer.. So simple I almost want to cry, but it’s great because the code works like a charm!

So here is what I did (you’ll laugh!):

Code added to the generic class

bool TypeValid() {     // Get the TypeCode from the Primitive Type     TypeCode code = Type.GetTypeCode(typeof(PrimitiveDataType));      // All of the TypeCode Enumeration refer Primitive Types     // with the exception of Object and Empty (Null).     // Since I am willing to allow Null Types (at this time)     // all we need to check for is Object!     switch (code)     {         case TypeCode.Object:             return false;         default:             return true;     } } 

Then a little utility method to check the type and throw an exception,

private void EnforcePrimitiveType() {     if (!TypeValid())         throw new InvalidOperationException(             "Unable to Instantiate SimpleMetadata based on the Generic Type of '" + typeof(PrimitiveDataType).Name +              "' - this Class is Designed to Work with Primitive Data Types Only."); } 

All that then needs to be done is to call EnforcePrimitiveType() in the classes constructors. Job done! 🙂

The only downside, it only throws an exception at runtime (obviously) rather than design time. But that’s no big deal and could be picked up with utilities like FxCop (which we don’t use at work).

Special thanks to Jon Limjap on this one!

  • 1 1 Answer
  • 1 View
  • 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. 2026-05-10T14:31:03+00:00Added an answer on May 10, 2026 at 2:31 pm

    Primitives appear to be specified in the TypeCode enumeration:

    Perhaps there is a way to find out if an object contains the TypeCode enum without having to cast it to an specific object or call GetType() or typeof()?

    Update It was right under my nose. The code sample there shows this:

    static void WriteObjectInfo(object testObject) {     TypeCode    typeCode = Type.GetTypeCode( testObject.GetType() );      switch( typeCode )     {         case TypeCode.Boolean:             Console.WriteLine('Boolean: {0}', testObject);             break;          case TypeCode.Double:             Console.WriteLine('Double: {0}', testObject);             break;          default:             Console.WriteLine('{0}: {1}', typeCode.ToString(), testObject);             break;         }     } } 

    It’s still an ugly switch. But it’s a good place to start!

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

Sidebar

Related Questions

Is there a way to enforce that a type parameter passed to an attribute
In SQL, is there a way to enforce that only one column out of
Is there any way to enforce that a method call in soap based wcf
Quick one: Is there any way to enforce types for variadic functions in PHP?
Is there a way to enforce variable to be one of multiple types? Something
Is there a way to enforce that a controller inherits from another one in
Is there a way to enforce constraint checking in MSSQL only when inserting new
Is there any way to enforce a template in Bugzilla to guide users fill
In java, is there a general way to enforce compile-time compilation? In a programming
Is there any way in Java to enforce a minimum window size? The last

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.