I have a .cs
file in a location. How do I read the file and extract only the properties from it? Is it possible to extract the properties without compiling the code? I tried with Assembly.LoadFile()
and Assembly.LoadFrom()
class but doesn't work!!! Here is the sample code
using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel;
using System.Xml.Serialization;
namespace namespace1.Did
{
public class Class1
{
#region Variables
private int _property1 = 14;
private int _property2 = 16;
#endregion
#region Methods
protected override void Initialize()
{
}
protected override void OnBarUpdate()
{
// Have some code in this which uses System.Drawing and System.Drawing.Drawing2D dll's
}
#endregion
#region Properties
[Description("Demo1")]
[GridCategory("Parameters")]
public int Property1
{
get { return _property1; }
set { _property1 = Math.Max(1, value); }
}
[Description("Demo2")]
[GridCategory("Parameters")]
public int Property2
{
get { return _property2; }
set { _property2 = Math.Max(1, value); }
}
#endregion
}
}
Actually I don't want to compile this code, since in case I am using some other dll, I need to add reference as parameter to the Csharpcodeprovider class dynamically. How do I get the properties only from this .cs
file?
You just need to read the file as plain text and parse the text using RegEx or whatever suits your needs. Since you have mentioned you cannot share the file structure(ridiculous as it sounds) you will need to work out mechanism for parsing and filtering property names by yourself.