0

I've previously asked a question "How to get the Type of an Array of Nullable Primitive?" How to get the Type of an Array of Nullable Primitive?.

In that question, I have a class in my c# program and I was able to use reflection to find out the properties name/type/array(or not)/nullable(or not).

Now, I want to do the same but the class is read in from a file (and not part of the c# program). I check online for some codes that I can compile the file using CSharpCompilation.Create() into an assembly. By trial and error, I manage to print out the name and type.

 Assembly a = Assembly.Load(ms.ToArray());
 var _prop = a.GetType("ClassName").GetProperties();

 foreach (var _p in _prop)
 {
      Console.WriteLine(_p.Name);
      Console.WriteLine(_p.GetMethod.ReturnType.Name);
      Console.WriteLine();
 }

I don't know if this is the correct way to get the information. But my problem now is that I cannot find out if the type is an array and/or a nullable type. In my previous question, I learnt that I can use

var type = _propertyType.GetElementType();
type = Nullable.GetUnderylingType(type) ?? type;
var UType = type.Name;

to get the nullable. In my current example, how do I know if the property is an array and/or nullable ? The package I'm using:

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Emit;
using System.Reflection;

Thanks.

EBDS
  • 1,244
  • 5
  • 16
  • Can you provide a error message or observed with expected behaviour? Reflection is a valid approach to get information after the compilation with `CSharpCompilation`. – thehennyy Jan 13 '23 at 14:57
  • I'm not getting an error. The above works. It's just that I don't know how to get the nullable and array info when the class is runtime compiled. The methods associated with the property looks different. And if I use Nullable.GetUnderylingType(type), I don't get a type when it's nullable which I did previously. – EBDS Jan 13 '23 at 14:59

0 Answers0