0

I have following code:

public class Processor
{
    private Query _query = 
        new SpecificQuery1();
    //OR
      //new SpecificQuery2();

   public void ProcessItem(dynamic dynamicResult)
   {
        //Can't use intellisense on dynamicResult

        var staticResult = dynamicResult as _query.GetSomeType();//Can't do it :(

        //Can use intellisense on staticResult
   }
}

and, surprisingly, it doesn't compile. Is there any way I could cast dynamic into var? I know it sounds insane, but this part will be edited a lot and if someone changes the QueryImplementation, he has to also change type in ProcessItem(). I want to reduce the number of steps to 1 - simply replace the SpecificQuery() and the type will change by itself.

So let me rephrase. I'd like to know if there is some way how to use intellisense on dynamicResult(or some of it's cast) based on which constructor is assigned to base class Query.

Thanks

EDIT : I'm sorry, I probably asked incorrectly. I understand what is dynamic and var. I didn't intent to use intellisense on dynamic. I didn't intent to really cast dynamic to var.

What I wanted to say is, that if I have compile-time knowledge of what type the dynamic will be (it is stored in Query implementation - it can be static, const whatever I want) - is there any way I can use this knowledge to enable intellisense in ProcessItem()?

Biggles
  • 1,306
  • 1
  • 12
  • 22
  • 4
    You should read something to understand the difference between var and dynamic. – looper Nov 19 '12 at 12:09
  • No, dynamic types can't be used with Intellisense in the way you want because their properties/method will be known only at run-time. You can't cast to "var" in that way. It's really insane...but you can do: "**var staticResult = (SpecificQuery1)dynamicResult;**" (note, please, that you were trying to cast again to an unknown type, resolved at run-time...) – Adriano Repetti Nov 19 '12 at 12:10
  • Just to clarify. I understand the difference between the two. I understand I can't use intellisence on dynamic - I know why. I will know always in compile-time, what Type will be used in ProcessItem so I thought there might be some way. – Biggles Nov 19 '12 at 12:14
  • `I will know always`, then why is it `dynamic`? – Tamara Wijsman Nov 19 '12 at 12:15
  • Your `as` cast above doesn't make sense. A cast changes the **compile-time** type to a specific type which has to be known compile-time. For example if you say `var resultCast = resultOrig as string;` then after that (and a `null` check) you can say stuff like `resultCast.Length` because the compile-time type of `resultCast` is `string`, and strings have lengths. However, the cast doesn't change the fact that there is only one object, and the object has only one runtime-type. `Object.RefereceEquals(resultOrig, resultCast)`. How could the compiler know what `_query.GetSomeType()` will return? – Jeppe Stig Nielsen Nov 19 '12 at 12:43
  • You say you know the difference yet this question sort of indicates you don't. If you don't actually intend on really casting a dynamic to a var why are you asking if its possible? – Security Hound Nov 19 '12 at 13:37

4 Answers4

4

The var contextual keyword is just syntactic sugar. There is no need to "cast" anything to it, as the variable declared with it is already strongly typed.

If the type of the result of the function is dynamic, so will the variable declared with var.

staticResult is of type dynamic:

var staticResult = dynamicResult;

You can't get intellisense on a dynamic type. If you know the type that you will get, then cast to it - that will give you access to intellisense.

var staticResult = (myType)dynamicResult;

Note that the above can easily cause runtime errors and exceptions that will crash the process.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
2

Please see this SO answer which explains the difference between var and dynamic in detail, in light of your question you should know that the compiler will know the type of var at compile time whereas dynamic can only be determined at runtime; hence, you can't assign a type casted from dynamic (to be determined at runtime) to var (to be determined at compile time).

So, why not keep it dynamic?

Community
  • 1
  • 1
Tamara Wijsman
  • 12,198
  • 8
  • 53
  • 82
2

No, you cannot do anything like that.

To begin with, you cannot cast something to var since var is not a type. Furthermore, casting can only be done to a type that is statically known; this means that the type you are casting to must be hardcoded, and cannot be the result of evaluating an expression (such as the method call _query.GetSomeType() in your example).

Jon
  • 428,835
  • 81
  • 738
  • 806
1

you could implement somethong like

public abstract class AbstractQuery
{
     AbstractQuery Create(dynamic result);
}
public class SpecificQuery1 : AbstractQuery
{
    new public SpecificQuery1 Create(dynamic result)
    {
       ...
    }
}

public void ProcessItem(dynamic dynamicResult)
{
    var staticResult = _query.Create(dynamicResult);
}

to convert from a dynamic to a typed result

Lukas Winzenried
  • 1,919
  • 1
  • 14
  • 22
  • And what would be the content of the Create() method? – Biggles Nov 19 '12 at 12:22
  • your specific implementation. Where does dynamicResult come from? If it's the result of a DB query you could consider to use an ORM tool to get a concrete implementation instead of a dynamic result. – Lukas Winzenried Nov 19 '12 at 12:27