Cast to a type from a generic class

JL.

I have a generic class that looks like this:

public interface IStationProperty
   {
      int Id { get; set; }
      string Desc { get; set; }
      object Value { get; }
      Type ValueType { get; }
   }

   [Serializable]
   public class StationProp<T> : IStationProperty
   {
      public StationProp()
      {
      }

      public StationProp(int id, T val, string desc = "")
      {
         Id = id;
         Desc = desc;
         Value = val;
      }

      public int Id { get; set; }
      public string Desc { get; set; }
      public T Value { get; set; }

      object IStationProperty.Value
      {
         get { return Value; }
      }

      public Type ValueType
      {
         get { return typeof(T); }
      }

The property that gets the type is:

public Type ValueType
      {
         get { return typeof(T); }
      }

So in my code, I have a loop pulling values (as string) from the db, here I want to do a type conversion (on the left side) so I can do a reliable value comparison.

I would like something like this:

var correctlyTypedVariable = (prop.ValueType) prop.Value; 

I know this kind of thing has to be possible.

rducom

You already have

public T Value { get; set; }

which returns the typed value. If on the following code, the prop object is of type IStationProperty

var correctlyTypedVariable = (prop.ValueType) prop.Value; 

then maybe your problem is on the interface : you should better use a generic one :

public interface IStationProperty
{
  int Id { get; set; }
  string Desc { get; set; } 
  Type ValueType { get; }
}

public interface IStationProperty<T> : IStationProperty
{ 
   T Value { get; } 
}

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Cast object to generic type at runtime

분류에서Dev

How to run method that returns an object of "Type" from generic class

분류에서Dev

How to run method that returns an object of "Type" from generic class

분류에서Dev

Generic class with explicitly type-safe taxonomy

분류에서Dev

Cast from unknown type in c#

분류에서Dev

How do I use an extended generic type's type in a class?

분류에서Dev

Return generic type from dictionary (else return a default generic value)

분류에서Dev

Returning a generic object on Java from abstract class

분류에서Dev

Using a bounded generic class to extend another generic class produces an unknown type

분류에서Dev

Spring AOP - using joinpoint to get generic class type (JAVA)

분류에서Dev

Java: Is it possible to compare a Class<?> object with a generic type parameter?

분류에서Dev

Generics class from a runtime type

분류에서Dev

to build a generic cast function in scala

분류에서Dev

Generic method with property expression "type arguments cannot be inferred from useage"

분류에서Dev

How do i dynamically cast a child class from its parent class?

분류에서Dev

Generic type conversion in generic method

분류에서Dev

Is it discouraged to down-cast a returned instance to gain functionality that is not exposed via the returned class type?

분류에서Dev

Decode Class from @encoded type string

분류에서Dev

Union type for generic param

분류에서Dev

Java Generic type identification

분류에서Dev

generic data type id

분류에서Dev

Issue with generic type bounds

분류에서Dev

FSharp: Type inference with generic

분류에서Dev

Generic Constructor of class

분류에서Dev

Generic class for operations on Number

분류에서Dev

Why do I need a cast to access the actual value from base class?

분류에서Dev

From an Interface how do I return a generic/dynamic type from a function

분류에서Dev

Cast implied unsigned integer type?

분류에서Dev

Class cast exception in jersey project?

Related 관련 기사

  1. 1

    Cast object to generic type at runtime

  2. 2

    How to run method that returns an object of "Type" from generic class

  3. 3

    How to run method that returns an object of "Type" from generic class

  4. 4

    Generic class with explicitly type-safe taxonomy

  5. 5

    Cast from unknown type in c#

  6. 6

    How do I use an extended generic type's type in a class?

  7. 7

    Return generic type from dictionary (else return a default generic value)

  8. 8

    Returning a generic object on Java from abstract class

  9. 9

    Using a bounded generic class to extend another generic class produces an unknown type

  10. 10

    Spring AOP - using joinpoint to get generic class type (JAVA)

  11. 11

    Java: Is it possible to compare a Class<?> object with a generic type parameter?

  12. 12

    Generics class from a runtime type

  13. 13

    to build a generic cast function in scala

  14. 14

    Generic method with property expression "type arguments cannot be inferred from useage"

  15. 15

    How do i dynamically cast a child class from its parent class?

  16. 16

    Generic type conversion in generic method

  17. 17

    Is it discouraged to down-cast a returned instance to gain functionality that is not exposed via the returned class type?

  18. 18

    Decode Class from @encoded type string

  19. 19

    Union type for generic param

  20. 20

    Java Generic type identification

  21. 21

    generic data type id

  22. 22

    Issue with generic type bounds

  23. 23

    FSharp: Type inference with generic

  24. 24

    Generic Constructor of class

  25. 25

    Generic class for operations on Number

  26. 26

    Why do I need a cast to access the actual value from base class?

  27. 27

    From an Interface how do I return a generic/dynamic type from a function

  28. 28

    Cast implied unsigned integer type?

  29. 29

    Class cast exception in jersey project?

뜨겁다태그

보관