locked
Cast generic type without knowing T? RRS feed

  • Question

  • User1099842428 posted

    I've got a simple class I use in a framework so the business objects can use different distinct identifiers (DB int, guid, key etc).

    public class Identifier<T>
    {
    	private T _value;
    	public Identifier(){
    		
    	}
    	public Identifier(T value)
    	{
    		Value = value;
    	}
    	public T Value
    	{
    		get { return _value; }
    		set { _value = value; }
    	}
    	/* implicit conversions */
    	public static implicit operator int(Identifier<T> d) 
    	{
    		return (int)(object)d.Value;  
    	}
    }

    I have implicit and explicit operators so that the follwing works:

    Identifier<int> GenericId = new Identifier<T>();
    //the cast is implicit so no need to use GenericId.Value
    Int32 id = GenericId;

    My issue is that I would like to detect when an object is an Identifier. I can use :

    if(value.GetType().GetGenericTypeDefinition() == typeof(Identifier<>))

    but i then can't get the Value property because I don't know T at run time This is a rough outline of what I'm trying to do:

    public object GetValue(object value){
      if(value.GetType().GetGenericTypeDefinition() == typeof(Identifier<>)){
        //how do I achieve this line because I don't know T?
        return object.value;
      }else{
        return object;
      }
    }

    Any ideas gratefully received :)






    Tuesday, May 7, 2013 4:21 PM

Answers

  • User1099842428 posted

    Managed to figure it out. 

    private object GetValue(object value)
    {
      Type t = value.GetType();
      if (t.IsGenericType)
      {
        if (t.GetGenericTypeDefinition() == typeof(Identifier<>)) //check the object is our type
        {
          //Get the property value
          return t.GetProperty("Value").GetValue(value, null);
        }
      }
      return value;
    }


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 8, 2013 5:50 AM

All replies

  • User1099842428 posted

    Managed to figure it out. 

    private object GetValue(object value)
    {
      Type t = value.GetType();
      if (t.IsGenericType)
      {
        if (t.GetGenericTypeDefinition() == typeof(Identifier<>)) //check the object is our type
        {
          //Get the property value
          return t.GetProperty("Value").GetValue(value, null);
        }
      }
      return value;
    }


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 8, 2013 5:50 AM
  • User-837620913 posted

    You could make an Interface that specifies the "Value" property, like this:

    interface IIdentity
    {
        object Value;
    }


    Then add a constraint to your class, like this:

    public class Identifier<T> : IIdentity


    Then you could cast it to see if it works:

    var temp = value as IIdentity;
    if(temp != null)
        return temp.Value;



    Wednesday, May 8, 2013 5:56 AM