Note that there are some explanatory texts on larger screens.

plurals
  1. POC# Explicit operator and Object
    primarykey
    data
    text
    <p><strong>QUESTION</strong></p> <p>Please take a look to the code first.</p> <p>Here is my custom class:</p> <pre><code>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); } ... } </code></pre> <p>And here is the test code I wrote:</p> <pre><code> 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. } </code></pre> <p>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?</p> <p>I have a code where EVERYTHING is object and it must take care of these conversations.</p> <p>Please tell me if you have any Idea.</p> <hr> <p><strong>AVOIDING DYNAMIC</strong></p> <p>Ok let change the sample in the way you can see what I exactly want to do and what is my situation.</p> <p>Here is the class I have:</p> <pre><code> class TEST { dynamic OBJECT; public void STORE&lt;T&gt;(ref T V) { this.OBJECT = V; } public T CONVERT&lt;T&gt;() { return (T)this.OBJECT; } } </code></pre> <p>And here is the test code:</p> <pre><code> float2D V = new float2D(1, 1); TEST t = new TEST(); t.STORE(ref V); Point P = t.CONVERT&lt;Point&gt;(); </code></pre> <p>Is there way I can drop dynamic from above class and keep it working? I really want to avoid .Net4/4.5</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload