简体   繁体   中英

Cast dynamic to var

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()?

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.

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?

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).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM