问题
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:
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
来源:https://stackoverflow.com/questions/1649264/how-to-make-a-type-safe-wrapper-around-variant-values