QUESTION
Please take a look to the code first.
Here is my custom class:
public class float2D
{
public float X { get; private set; }
public float Y { get; private set; }
public float2D(float x, float y)
{
this.X = x;
this.Y = y;
}
public static explicit operator Point(float2D x)
{
return new Point((int)x.X, (int)x.Y);
}
...
}
And here is the test code I wrote:
private void TEST()
{
float2D V = new float2D(1, 1);
Point P = Point.Empty;
P = (Point)V; // Works
P = (Point)(V as object); // Specified cast is not valid.
}
As you can see it failed to convert the value when value type is not known. I believe this happens because it search in Object class for operand not in real type. How can I solve this problem?
I have a code where EVERYTHING is object and it must take care of these conversations.
Please tell me if you have any Idea.
AVOIDING DYNAMIC
Ok let change the sample in the way you can see what I exactly want to do and what is my situation.
Here is the class I have:
class TEST
{
dynamic OBJECT;
public void STORE<T>(ref T V)
{
this.OBJECT = V;
}
public T CONVERT<T>()
{
return (T)this.OBJECT;
}
}
And here is the test code:
float2D V = new float2D(1, 1);
TEST t = new TEST();
t.STORE(ref V);
Point P = t.CONVERT<Point>();
Is there way I can drop dynamic from above class and keep it working? I really want to avoid .Net4/4.5
As stated by other answers, you can't do this because you're trying to apply runtime casting to compile time conversion operations.
If you want to avoid
dynamic
because you don't want to use .NET 4.0's DLR, you could use reflection to find the conversion operators yourself. Can't comment on performance for your particular application doing this however:Or for .NET 3.5 with LINQ:
Usage:
You can add "op_Implicit" as well if you wish to convert via implicit operators.
Another option if you want to avoid reflection is to pre-register the conversion functions, some casting and type lookups to determine which conversion operator to use.
Just a caveat, the solution here has a couple issues (thread safety, assumption that conversion functions exist, registering colliding/duplicate conversion functions throws errors) so use at your own risk or use it as a guide to modify as it suits your needs.
Basic gist is to define a simple converter to wrap the conversion functions themselves:
Then a utility that you can register these conversions with:
For usage, first you must register the conversion functions you expect to use in your application (ideally perform the registration when your application starts up to avoid threading issues):
Then your conversion usage:
Not sure about the performance of one over the other for your particular application usage. The first sample is a bit more flexible as it performs the check at runtime and you don't have to pre-register the conversions you expect to use. The second might be a bit more stable as you only register the conversions you expect to use and there is no reflection, just casting and dictionary lookups.