All kinds of problems you may encounter when using dynamic keyword type method call in C #

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/WPwalter/article/details/102634877

You can use dynamicto define a variable or field, then you can be like a weakly typed language, like calling various methods of the instance, just as you knew from the start as all properties and methods of this type.

However, improper use will encounter a variety of problems, all kinds of problems you may encounter This collection of course, help you get rid of them.


Getting Started

dynamic It can be used:

dynamic foo = GetSomeInstance();
foo.Run("欢迎访问吕毅(lvyi)的博客:blog.walterlv.com");

object GetSomeInstance()
{
    return 诡异的东西;
}

We GetSomeInstanceobviously returns object, we have real class method can be called.

Next, you use about dynamicissues that may be encountered and solutions.

Compile Error: Missing compiler required member

You first introduced in your project dynamicafter the keyword, the compiler error occurs, suggesting that "the lack of compiler requires members."

error CS0656: lack of compiler requires that members "Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create"

For .NET Core or .NET Standard Project

NuGet need to install the following two packages for your project:

Refer to two NuGet package

So your project which will be more than two references:

    <Project Sdk="Microsoft.NET.Sdk">

      <PropertyGroup>
        <TargetFrameworks>netstandard2.0;net48</TargetFrameworks>
      </PropertyGroup>

      <ItemGroup>
++      <PackageReference Include="Microsoft.CSharp" Version="4.5.0" />
++      <PackageReference Include="System.Dynamic.Runtime" Version="4.3.0" />
      </ItemGroup>

    </Project>

For the .NET Framework Project

You need to reference Microsoft.CSharp:

Add Reference

Reference Microsoft.CSharp

So your project which will be more of a quote:

    <Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">

      <PropertyGroup>
        <TargetFramework>net48</TargetFramework>
      </PropertyGroup>

      <ItemGroup>
++      <Reference Include="Microsoft.CSharp" />
      </ItemGroup>

    </Project>

Exception: "{0}" does not contain "{1}" is defined as

{0} 是类型名称,而 {1} 是使用 dynamic 访问的属性或者方法的名称。

比如,我试图从某个 Attribute 中访问到 Key 属性的时候会抛出以下异常:

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:““System.Attribute”未包含“Key”的定义”

出现此异常的原因是:

  • dynamic 所引用的对象里面,没有签名相同的 public 的属性或者方法

于是,如果你确认你的类型里面是有这个属性或者方法的话,那么就需要注意需要将此成员改成 public 才可以访问。


参考资料


我的博客会首发于 https://blog.walterlv.com/,而 CSDN 会从其中精选发布,但是一旦发布了就很少更新。

如果在博客看到有任何不懂的内容,欢迎交流。我搭建了 dotnet 职业技术学院 欢迎大家加入。

Creative Commons License

本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。欢迎转载、使用、重新发布,但务必保留文章署名吕毅(包含链接:https://walterlv.blog.csdn.net/),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我联系

Guess you like

Origin blog.csdn.net/WPwalter/article/details/102634877