setting value to the instance of primitive types

Posted by Samreen on Stack Overflow See other posts from Stack Overflow or by Samreen
Published on 2010-06-17T05:04:12Z Indexed on 2010/06/17 5:13 UTC
Read the original article Hit count: 272

Filed under:
|
|
|

I have a function which performs some operation i.e. fetches some data from database. The generic it takes is primitive type always i.e. int, char, bool, string etc. I can easily create its instance. But I can't set the value that I have fetched from database to this instance.

public T PerformOperation<T>()
{    
    object instance = (T)Activator.CreateInstance(typeof(T));

    object result=FetchData();

    instance = (T)result; //It gives error on this statement
}

The function is called as:

int result = PerformOperation<int>();

Is there some way of casting the object Type to any generic type which is always primitive?

© Stack Overflow or respective owner

Related posts about c#

Related posts about type