简体   繁体   English

var 关键字可以将变量类型更改为动态吗?

[英]var keyword can change a variable type to dynamic?

I have a service for Entity1 whose name is Entity1Service and it contains a method GetBySerial which is responsible for fetch the appropriate document from the database (I used LiteDb ) and returns it:我有一个服务Entity1他的名字是Entity1Service ,它包含一个方法GetBySerial它负责从数据库(我用取合适的文档LiteDb ),并返回它:

public class Entity1Service
{
    private readonly LiteCollection<Entity1> _entity1Collection;
    private readonly LiteDatabase _db;

    public Entity1Service()
    {
        _db = new LiteDatabase(Constants.DbFileName);
        _entity1Collection = _db.GetCollection<Entity1>(nameof(Entity1));
    }
    public Entity1 GetBySerial(string serial)
    {
        var q = Query.EQ(nameof(Entity1.Serial), serial);

        lock (Constants.DbLockObj)
        {
            return _entity1Collection.Find(q).SingleOrDefault();
        }
    }
}

If I use this method like this:如果我像这样使用这种方法:

Entity1Service _entity1Service = new Entity1Service();
dynamic MyDynamic = new System.Dynamic.ExpandoObject();
MyDynamic.Serial = "serial";
var e1 = _entity1Service.GetBySerial(MyDynamic.Serial);

the e1 will be a dynamic variable. e1将是一个动态变量。 But, if I use it like this:但是,如果我像这样使用它:

Entity1 e1 = _entity1Service.GetBySerial("serial");

Its type will be Entity1 as expected.正如预期的那样,它的类型将是Entity1 I can't figure out why the compiler acts like this.我不明白为什么编译器会这样。

I face this behavior in a console application, dotnet core v. 2.1 and C# v. 7.3我在控制台应用程序 dotnet core v. 2.1 和 C# v. 7.3 中遇到这种行为

Update:更新:

Thanks to Jon Skeet, I tried to make a "minimal reproducible example" as he mentioned in a comment.感谢 Jon Skeet,正如他在评论中提到的那样,我尝试制作一个“最小可重复示例”。 So, during this process, I found that the problem is happened because of a code that I removed from the code for simplicity.所以,在这个过程中,我发现问题的发生是因为为了简单起见,我从代码中删除了一段代码。 So, the problem exactly happens when I pass a dynamic value as the argument to the method.因此,当我将动态值作为参数传递给方法时,问题就会发生。 So I edited the first code section.所以我编辑了第一个代码部分。

This isn't really about var .这不是真的关于var That just showed up something that was already going on in your explicitly-typed code.这只是显示了在您的显式类型代码中已经发生的事情。 This is about how dynamic typing works.这是关于动态类型的工作原理。 The result of almost any expression involving dynamic is deemed to be dynamic .几乎任何涉及dynamic表达式的结果都被认为是dynamic

So in this code:所以在这段代码中:

Entity1Service _entity1Service = new Entity1Service();
dynamic MyDynamic = new System.Dynamic.ExpandoObject();
MyDynamic.Serial = "serial";
var e1 = _entity1Service.GetBySerial(MyDynamic.Serial);

... the compile-time type of MyDynamic.Serial is dynamic , and the call to _entity1Service.GetBySerial is dynamically bound, with a result of dynamic . ... MyDynamic.Serial的编译时类型是dynamic ,对_entity1Service.GetBySerial的调用是动态绑定的,结果是dynamic When you explicitly type e1 as Entity1 , you're effectively adding a cast after the call.当您将e1显式键入为Entity1 ,您实际上是在调用后添加了一个演员表。

If you just make sure everything in the expression is statically typed, the result will have the static type you expect.如果您只是确保表达式中的所有内容都是静态类型的,那么结果将具有您期望的静态类型。 For example, you could cast the argument:例如,您可以转换参数:

Entity1Service _entity1Service = new Entity1Service();
dynamic MyDynamic = new System.Dynamic.ExpandoObject();
MyDynamic.Serial = "serial";
var e1 = _entity1Service.GetBySerial((string) MyDynamic.Serial);

Or you could use a separate local variable:或者您可以使用单独的局部变量:

Entity1Service _entity1Service = new Entity1Service();
dynamic MyDynamic = new System.Dynamic.ExpandoObject();
MyDynamic.Serial = "serial";
string serial = MyDynamic.Serial;
var e1 = _entity1Service.GetBySerial(serial);

Either way, the _entity1Service.GetBySerial call is now statically bound - which is almost certainly what you want - and the type of e1 will be Entity1 .无论哪种方式, _entity1Service.GetBySerial调用现在都是静态绑定的——这几乎肯定是你想要的——并且e1的类型将是Entity1

a brief history of var; var的简要历史;

var was introduced in C#3.0. var是在C#3.0中引入的。 Its purpose is to “implicitly” tell the compiler what type of variable it is. 其目的是“隐式”告诉编译器变量的类型。 This means that the variable type is determined by the compiler at compilation time. 这意味着变量类型由编译器在编译时确定。

dynamic keyword is more used for inter language field information. 动态关键字更多地用于语言间字段信息。 It should be discouraged. 不应该这样。 Its like an object type without the need for cast. 它就像对象类型,无需强制转换。

Without looking at the code, its hard to tell. 如果不看代码,很难分辨。 Its more of a guess. 它更多的是猜测。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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