PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, July 19, 2022

[FIXED] How to Identify the Data Type of Generic Class/Method

 July 19, 2022     c#, generics, integer, string, types     No comments   

Issue

Here is my code, Which I tried to identify the Generic method data type.

  1. First If condition is used to identify the primitive data type.

  2. Second If condition is used for T is generic type or class.

    public T ToObject<T>()
         {
             //T is : int,int?,string,float,decimal 
             if (typeof(T).IsValueType)
             {
                 //TO DO : Return Value Type
             }    
             //T is Generic List<User> 
             if (typeof(T).IsGenericType)
             {
                 //TO DO : Return List of Object
             }
             else
             {
                 //T is User 
                 //TO DO : Return Object
             }                
             return default(T);
         }
    

Solution

string is not a value type, so your first condition breaks straight away.

Its really unclear what you are asking and this seems strange to say the least. However, in your first example IsValueType just wont work, what about structs?how are you going to differentiate? You can use the property IsPrimitive, but be careful because there are some types that we can think that are primitives, but they aren´t, for example Decimal, String, DateTime, TimeSpan, and so many more.

if (t.IsPrimitive || t == typeof(Decimal) || t == typeof(String) || ... )
{
    // Is Primitive, or Decimal, or String
}

Yet still, this code wont work for nullable types, they wont be flagged as primitive.

I really think you need to reconsider your design, you are going to have to make so many exceptions and it will break all over the place.

Is generics really what you want here?

FWIW, i think you have a design or architectural problem somewhere else that leads you to need to use a generic method like this, or you have unrealistic expectations about what types are and can be categorised as.

However, if you really have your heart set on this, you could tentatively use something like the following for "primitive like" types

Updated thanks to xanatos sugestions

private static readonly Type[] _types = {
      typeof(string),
      typeof(decimal),
      typeof(DateTime),
      typeof(DateTimeOffset),
      typeof(TimeSpan),
      typeof(Guid)
   };

public static bool IsSimpleType(Type type)
{
   Type baseType;
   return type.IsPrimitive || 
          type.IsEnum || 
          _types.Contains(type) || 
          ((baseType = Nullable.GetUnderlyingType(type)) != null && 
           IsSimpleType(baseType));
}

Disclaimer, I take no responsibility for the people you maim and injure with this code



Answered By - TheGeneral
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing