Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Using dynamic keyword not working

Discussion in 'Scripting' started by RickP, Aug 26, 2017.

  1. RickP

    RickP

    Joined:
    Apr 4, 2010
    Posts:
    262
    I'm trying to use dynamic keyword and I'm getting the compiler error from VS 2017:

    Error CS0656 Missing compiler required member 'Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create'

    I followed the below link but it didn't work for the dynamic keyword. The other features on this post don't give me any errors though (Index Initializers, String Interpolation) so it seems like I am using C# 6 features but dynamic keyword just doesn't compile for me.

    https://stackoverflow.com/questions/45616562/using-dynamic-keyword-in-unity

    Before I installed VS Unity Tools I was able to add a reference to make this work but then I wasn't able to debug without the tools. When I installed the tools now it doesn't let you add references. So I'm kind of stuck. Any help would be great!
     
    JDCaptainAmerica and AbleArcher like this.
  2. rastlin

    rastlin

    Joined:
    Jun 5, 2017
    Posts:
    127
    Just encountered this issue as well on 2018.2.4.

    The solution is not complicated, we need to modify csproj file generated by VSTU, and add Microsoft.CSharp. I wrote simple project generation hook to automate this:

    Code (csharp):
    1.  
    2. [InitializeOnLoad]
    3. public class ProjectFileHook
    4. {
    5.     private static string msBuildNs = "http://schemas.microsoft.com/developer/msbuild/2003";
    6.  
    7.     // necessary for XLinq to save the xml project file in utf8
    8.     class Utf8StringWriter : StringWriter
    9.     {
    10.         public override Encoding Encoding
    11.         {
    12.             get { return Encoding.UTF8; }
    13.         }
    14.     }
    15.  
    16.     static ProjectFileHook()
    17.     {
    18.         ProjectFilesGenerator.ProjectFileGeneration += (string name, string content) =>
    19.         {
    20.             if (name.Contains("Editor"))
    21.             {
    22.                 // parse the document and make some changes
    23.                 var document = XDocument.Parse(content);
    24.  
    25.                 XElement itemGroup = new XElement(XName.Get("ItemGroup", msBuildNs));
    26.  
    27.                 XElement reference = new XElement(XName.Get("Reference", msBuildNs));
    28.                 reference.Add(new XAttribute(XName.Get("Include"), "Microsoft.CSharp"));
    29.  
    30.                 itemGroup.Add(reference);
    31.  
    32.                 document.Root.LastNode.AddAfterSelf(itemGroup);
    33.  
    34.                 // save the changes using the Utf8StringWriter
    35.                 var str = new Utf8StringWriter();
    36.                 document.Save(str);
    37.  
    38.                 return str.ToString();
    39.             }
    40.  
    41.             return content;
    42.         };
    43.     }
    44. }
    45.  
    This add missing reference only to Editor assemblies, and resolves compilation error in Visual Studio, I don't think dynamic is supported on all the platforms.

    It would be actually nice if VSTU team could add this reference to their csproj file generation routine.
     
    TheSlate and JohnGray like this.
  3. sixolar

    sixolar

    Joined:
    Jan 8, 2014
    Posts:
    37
    Hi,

    I just can't get this to work, the line is not added to the csproj... I tried adding the reference "by hand", and it compile but unity still show the error in the console and if I close and reopen the project, it removes the reference.

    What am I missing??
     
  4. rastlin

    rastlin

    Joined:
    Jun 5, 2017
    Posts:
    127
    Hi sixolar,

    The script is meant to fix only Editor assemblies, there is a condition in line 20.

    I'm not sure if dynamic is supported in a build assemblies, that's why I added the condition., if your assembly that you are trying to fix is not Editor-only you can try to comment the mentioned line and see the effects, however I encourage you to test the build result after that.
     
    Last edited: Aug 23, 2018
  5. sixolar

    sixolar

    Joined:
    Jan 8, 2014
    Posts:
    37
    Thanks for the reply, I can't get this to work even in the editor, I have exactly the same error as the original post in the editor's console, so I can't hit play nor build
     
  6. sixolar

    sixolar

    Joined:
    Jan 8, 2014
    Posts:
    37
    Well I got it to work by just importing Microsoft.CSharp.dll directly in the assets...