C
C#13mo ago
soziapath

❔ Roslyn compilation error (dynamic keyword)

Hello everyone, I'm pretty new to C# and had to start using it due to my new job. In the project i'm working on we use quite a lot of autogenerated code that is compiled using roslyn. So I decided to look at it some more and try using it in a couple example projects. But I've encountered a problem compiling code in roslyn that uses dynamic objects. The following code gives the compile error:
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Dynamic;

namespace Autogenerated;
public class _t1
{
private dynamic _wrappedObject;
public _t1(object wrappedObject)
{
_wrappedObject = wrappedObject;
}
public void Test()
{
_wrappedObject.Test();
}
public void Some()
{
_wrappedObject.Some();
}
}
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Dynamic;

namespace Autogenerated;
public class _t1
{
private dynamic _wrappedObject;
public _t1(object wrappedObject)
{
_wrappedObject = wrappedObject;
}
public void Test()
{
_wrappedObject.Test();
}
public void Some()
{
_wrappedObject.Some();
}
}
And the error message is:
(9,13): error CS1980: Cannot define a class or member that utilizes 'dynamic' because the compiler required type 'System.Runtime.CompilerServices.DynamicAttribute' cannot be found. Are you missing a reference?
(9,13): error CS1980: Cannot define a class or member that utilizes 'dynamic' because the compiler required type 'System.Runtime.CompilerServices.DynamicAttribute' cannot be found. Are you missing a reference?
7 Replies
mtreit
mtreit13mo ago
That code compiles fine for me, you've got something else going on. What version of .NET are you targeting?
soziapath
soziapathOP13mo ago
It compiles fine itself, but as soon as I try compile it CSharpCompilation it fails. But home atm, but as soon as I am I can send the compilation code. I tried .NET 6, 7 and 8 with the same result. Just created a new program to test it, same thing:
using System;
using System.IO;
using System.Reflection;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Emit;

class Program
{
static void Main()
{
string sourceCode = @"
using System;
using System.Reflection;
using System.Runtime.CompilerServices;

namespace Autogenerated;
public class _t1
{
private dynamic _wrappedObject;
public _t1(object wrappedObject)
{
_wrappedObject = wrappedObject;
}
public void Test()
{
_wrappedObject.Test();
}
public void Some()
{
_wrappedObject.Some();
}
};";
SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(sourceCode);

string assemblyName = "MyAssembly.dll";
MetadataReference[] references = new MetadataReference[]
{
MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
MetadataReference.CreateFromFile(typeof(Console).Assembly.Location),
};

CSharpCompilation compilation = CSharpCompilation.Create(assemblyName)
.WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary))
.AddReferences(references)
.AddSyntaxTrees(syntaxTree);

using (var ms = new MemoryStream())
{
EmitResult emitResult = compilation.Emit(ms);

if (emitResult.Success)
{
ms.Seek(0, SeekOrigin.Begin);

var assembly = Assembly.Load(ms.ToArray());
Type programType = assembly.GetType("Program");
MethodInfo mainMethod = programType.GetMethod("Main");
mainMethod.Invoke(null, null);
}
else
{
foreach (var diagnostic in emitResult.Diagnostics)
{
Console.WriteLine(diagnostic);
}
}
}
}
}
using System;
using System.IO;
using System.Reflection;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Emit;

class Program
{
static void Main()
{
string sourceCode = @"
using System;
using System.Reflection;
using System.Runtime.CompilerServices;

namespace Autogenerated;
public class _t1
{
private dynamic _wrappedObject;
public _t1(object wrappedObject)
{
_wrappedObject = wrappedObject;
}
public void Test()
{
_wrappedObject.Test();
}
public void Some()
{
_wrappedObject.Some();
}
};";
SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(sourceCode);

string assemblyName = "MyAssembly.dll";
MetadataReference[] references = new MetadataReference[]
{
MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
MetadataReference.CreateFromFile(typeof(Console).Assembly.Location),
};

CSharpCompilation compilation = CSharpCompilation.Create(assemblyName)
.WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary))
.AddReferences(references)
.AddSyntaxTrees(syntaxTree);

using (var ms = new MemoryStream())
{
EmitResult emitResult = compilation.Emit(ms);

if (emitResult.Success)
{
ms.Seek(0, SeekOrigin.Begin);

var assembly = Assembly.Load(ms.ToArray());
Type programType = assembly.GetType("Program");
MethodInfo mainMethod = programType.GetMethod("Main");
mainMethod.Invoke(null, null);
}
else
{
foreach (var diagnostic in emitResult.Diagnostics)
{
Console.WriteLine(diagnostic);
}
}
}
}
}
Angius
Angius13mo ago
Jesus, seeing this thread I thought it's an issue with source generators... turns out it's an issue with compiling a string...? Who-wee
soziapath
soziapathOP13mo ago
sorry to disappoint you...
Angius
Angius13mo ago
Nah, I'm not disappointed, it's more like... seeing someone drive a car with square wheels lol, my condolences that you have to work with a codebase like that
soziapath
soziapathOP13mo ago
Yeah, it's not the nicest to work with, but at least it's paid well^^
Accord
Accord13mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server