Generic type checking
Asked Answered
V

9

72

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!! :D


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!

Vashti answered 12/8, 2008 at 15:7 Comment(1)
You could also call the check in your static constructor, so it's called only once per type used as generic argument.February
E
41

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!

Environmentalist answered 12/8, 2008 at 15:21 Comment(4)
Well, I don't know how but a main point of contention between objects and primitves is that most primitives are actually structs, not classes. I'll look for a way to figure them out programmatically :)Environmentalist
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 dont want to deal with (but then you need to worry about the structs that are used such as Size ).. Interesting problem no? :) EDIT: Ah yes, here it is: where T : struct Taken from MSDN.Vashti
I'm curious.. I wonder if this could be done in .NET 3.x using extension methods.. Create an interface.. 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? Sad news is I am working in framework 2!! :D EDIT: Leaving the office in like 5 mins, will pick this up when I get back home! :DVashti
> public class Class1<GenericType> where > GenericType : struct { } > > This one seemed to do the job.. The problem I see with that implementation is that if you have a custom struct it will still come across as a primitive, which is not the case.Environmentalist
D
73
public class Class1<GenericType> where GenericType : struct
{
}

This one seemed to do the job..

Dimeter answered 12/8, 2008 at 15:11 Comment(0)
E
41

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!

Environmentalist answered 12/8, 2008 at 15:21 Comment(4)
Well, I don't know how but a main point of contention between objects and primitves is that most primitives are actually structs, not classes. I'll look for a way to figure them out programmatically :)Environmentalist
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 dont want to deal with (but then you need to worry about the structs that are used such as Size ).. Interesting problem no? :) EDIT: Ah yes, here it is: where T : struct Taken from MSDN.Vashti
I'm curious.. I wonder if this could be done in .NET 3.x using extension methods.. Create an interface.. 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? Sad news is I am working in framework 2!! :D EDIT: Leaving the office in like 5 mins, will pick this up when I get back home! :DVashti
> public class Class1<GenericType> where > GenericType : struct { } > > This one seemed to do the job.. The problem I see with that implementation is that if you have a custom struct it will still come across as a primitive, which is not the case.Environmentalist
O
23

Pretty much what @Lars already said:

//Force T to be a value (primitive) type.
public class Class1<T> where T: struct

//Force T to be a reference type.
public class Class1<T> where T: class

//Force T to be a parameterless constructor.
public class Class1<T> where T: new()

All work in .NET 2, 3 and 3.5.

Overarm answered 12/8, 2008 at 15:43 Comment(0)
M
4

If you can tolerate using factory methods (instead of the constructors MyClass you asked for) you could always do something like this:

class MyClass<T>
{
  private readonly T _value;

  private MyClass(T value) { _value = value; }

  public static MyClass<int> FromInt32(int value) { return new MyClass<int>(value); }
  public static MyClass<string> FromString(string value) { return new MyClass<string>(value); }
  // etc for all the primitive types, or whatever other fixed set of types you are concerned about
}

A problem here is that you would need to type MyClass<AnyTypeItDoesntMatter>.FromInt32, which is annoying. There isn't a very good way around this if you want to maintain the private-ness of the constructor, but here are a couple of workarounds:

  • Create an abstract class MyClass. Make MyClass<T> inherit from MyClass and nest it within MyClass. Move the static methods to MyClass. This will all the visibility work out, at the cost of having to access MyClass<T> as MyClass.MyClass<T>.
  • Use MyClass<T> as given. Make a static class MyClass which calls the static methods in MyClass<T> using MyClass<AnyTypeItDoesntMatter> (probably using the appropriate type each time, just for giggles).
  • (Easier, but certainly weird) Make an abstract type MyClass which inherits from MyClass<AnyTypeItDoesntMatter>. (For concreteness, let's say MyClass<int>.) Because you can call static methods defined in a base class through the name of a derived class, you can now use MyClass.FromString.

This gives you static checking at the expense of more writing.

If you are happy with dynamic checking, I would use some variation on the TypeCode solution above.

Mayfield answered 16/9, 2008 at 5:52 Comment(0)
K
3

You can simplify the EnforcePrimitiveType method by using typeof(PrimitiveDataType).IsPrimitive property. Am I missing something?

Kirkkirkcaldy answered 19/10, 2008 at 22:38 Comment(1)
Passing a string into that statement returns False, and the poster specified he needed string to return True. .NET considers char to be a primitive, but not string.Daryl
L
3

@Rob, Enum's will slip through the TypeValid function as it's TypeCode is Integer. I've updated the function to also check for Enum.

Private Function TypeValid() As Boolean
    Dim g As Type = GetType(T)
    Dim code As TypeCode = Type.GetTypeCode(g)

    ' All of the TypeCode Enumeration refer Primitive Types
    ' with the exception of Object and Empty (Nothing).
    ' Note: must also catch Enum as its type is Integer.
    Select Case code
        Case TypeCode.Object
            Return False
        Case Else
            ' Enum's TypeCode is Integer, so check BaseType
            If g.BaseType Is GetType(System.Enum) Then
                Return False
            Else
                Return True
            End If
    End Select
End Function
Luxemburg answered 26/3, 2009 at 17:8 Comment(0)
E
3

Having a similar challenge, I was wondering how you guys felt about the IConvertible interface. It allows what the requester requires, and you can extend with your own implementations.

Example:

    public class MyClass<TKey>
    where TKey : IConvertible
{
    // class intentionally abbreviated
}

I am thinking about this as a solution, all though many of the suggested was part of my selection also.

My concern is - however - is it misleading for potential developers using your class?

Cheers - and thanks.

Eugenol answered 27/7, 2012 at 14:47 Comment(0)
A
2

Use a custom FxCop rule that flags undesirable usage of MyClass<>.

Aristotelian answered 12/8, 2008 at 15:21 Comment(0)
H
0

In dotnet 6, I encountered this error when using struct:

The type 'string' must be a non-nullable value type in order to use it as parameter 'T'

So I use IConvertible instead

var intClass = new PrimitivesOnly<int>();
var doubleClass = new PrimitivesOnly<double>();
var boolClass = new PrimitivesOnly<bool>();
var stringClass = new PrimitivesOnly<string>();
var myAwesomeClass = new PrimitivesOnly<MyAwesomeClass>(); // illegal

// The line below encounter issue when using "string" type
// class PrimitivesOnly<T> where T : struct
class PrimitivesOnly<T> where T : IConvertible
{
    
}

class MyAwesomeClass
{
}
Huai answered 21/5, 2022 at 19:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.