views:

152

answers:

1

I'm working with a OPC Server control that stores data tags as variant types, described by System.Runtime.InteropServices.VarEnum. These types include the following, VT_BSTR (string), VT_I2 (short) and VT_I4 (long).

All these values are stored by the server as objects and then I have to cast to the correct value when I fetch them.

I know that I can do something like the following:

object tagValue = (object)"testing"; //this would be returned from a function rather than created this way!!
var typedVariant = new TypedVariant<string>(tagValue);
string actualString = typedVariant.Value;

Where TypedVariant is a generic class something like this:

class TypedVariant<T> where T : class, struct
{          
    public TypedVariant(object variant)
    {
        Value = variant as T;
    }

    public T Value { private set; get; }

    public static implicit operator TypedVariant<T> (T m)
    {
        // code to convert from TypedVariant<T> to T
        return new TypedVariant<T>(m);
    }

    public static implicit operator T (TypedVariant<T> m)
    {
        // code to convert from T to TypedVariant<T>
        return m.Value;
    }  
}

But is there any way I can do it all at runtime, i.e. something like the following:

TypedVariant<> typedVariant = TypedVariant.Create(VarEnum.VT_BSTR, tagValue);
//typedVariant should now be of type TypedVariant<string>

Obviously this code won't compile, but can it be done like this?

Update: as per the suggestion from @Konamiman, I've changed the class to allow implicit casting. So you can now write this code and it's all typesafe, so you can't store the variant in a different type than the one it was created with.

object objectStr = (object)"testing"; //created this way just for testing        
TypedVariant<string> typedVariant = (string)objectStr;
string actualString = typedVariant;
+1  A: 

I think that the TypedVariant wrapper is a good idea, you could extend it to override the implicit conversion operator from/to the wrapped type and then you would be able to use it more transparently, such as:

var typedVariant = (string)tagValue;
string actualString = typedVariant;

See here: "implicit" keyword reference at MSDN

Konamiman
That's a good idea, thanks for that. Since I posted the question I've been wondering if I really want to decide it all at run-time anyway. I know the tags and their data-type before hand so having it all known at compile time isn't such a big problem.But I'll wait and see if there are any other suggestions.
Matt Warren