0

Possible Duplicate:
Parser for C#

In C# How To Read Entities from file(s) in a folder and read its Attributes. Example In Folder Companies.Entites if there are three entity files say

Company.cs
Office.cs
Contacts.cs

then how to read each file and process its attributes one by one.

Community
  • 1
  • 1
Rajesh Nagda
  • 131
  • 2
  • 9
  • 1
    Did you try to do it? Show us what you tried. – Zabavsky Dec 06 '12 at 14:23
  • I Havent tried as I does not know the right way. Only knows that it can be done from reflections. I wanted only simple thing to read a file find the class name and then process each attribute one by one. Thanks in advance – Rajesh Nagda Dec 06 '12 at 14:29
  • What do you mean by attributes? Class attributes? File attributes? – Sergey Berezovskiy Dec 06 '12 at 14:48
  • Class attributes. Eg. If Company Class contains property like Name, InclYear then i want all these property with there datatype. I want to process each attribute seperately – Rajesh Nagda Dec 06 '12 at 14:51

1 Answers1

2

Read you source files:

List<string> sources = new List<string>();

foreach (string file in Directory.GetFiles(path, "*.cs"))            
    sources.Add(File.ReadAllText(file));

Create compiler parameters:

CompilerParameters parameters = new CompilerParameters();
parameters.GenerateExecutable = false;
parameters.GenerateInMemory = true;

Add references to required libraries:

parameters.ReferencedAssemblies.Add("mscorlib.dll"); 
parameters.ReferencedAssemblies.Add("System.dll"); 
// etc

Compile your source to in-memory assembly:

var provider = new CSharpCodeProvider();
var results = provider.CompileAssemblyFromSource(parameters, sources.ToArray());

Verify compilation successful:

if (results.Errors.HasErrors)
{
    // display results.Errors
}

And use Reflection to read compiled types info:

var assembly = results.CompiledAssembly;
var types = assembly.GetTypes();

foreach (Type type in types)
{
    string name = type.Name;
    var properties = type.GetProperties(); // public properties
    // etc
}
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • The compilation was successfull but was giving exeption on line var types = assembly.GetTypes(); Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information. – Rajesh Nagda Dec 06 '12 at 15:37
  • Could not load file or assembly 'SaiAdinath.Core.Entities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified. Fusion Log: === Pre-bind state information === LOG: User = IMANDIASIA\rajesh LOG: DisplayName = SaiAdinath.Core.Entities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null (Fully-specified) LOG: Appbase = file:///D:/Data/New1/TestProject/ReadClassAndProperty/bin/Debug/ LOG: Initial PrivatePath = NULL Calling assembly : (Unknown). – Rajesh Nagda Dec 06 '12 at 15:46
  • 1
    Thanks a lot. The problem is solved as I copied the required dll in bin\debug folder. Once again Thanks @lazyberezovsky – Rajesh Nagda Dec 06 '12 at 15:51
  • @RajeshNagda sorry, was afk. Glad that problem was solved – Sergey Berezovskiy Dec 06 '12 at 20:24
  • The type or namespace name 'Linq' does not exist in the namespace 'System' (are you missing an assembly reference?) @Sergey Berezovskiy – Suman Banerjee Jan 16 '14 at 11:14