2009-05-14 3 views
2

ROWLEX 라이브러리를 사용하여 RDF-s를 처리했습니다. OwlGrinder.exe라는 designtime GUI 도구와 함께 제공되며,이 도구는 내 OWL 온톨로지에서 C# 도우미 클래스 (정확한 .NET 어셈블리)를 생성 할 수 있습니다. 런타임에 프로그래밍 방식으로 동일한 작업을 수행 할 수 있는지 여부를 아는 사람이 있는지 궁금합니다.ROWLEX를 사용하여 OWL 파일에서 프로그래밍 방식으로 어셈블리 생성

답변

4

ROWLEX가 방금 오픈 소스가되었으므로 이제 OwlGrinder.exe 코드를보고 거기에서 코드를 복사 할 수 있습니다.

private NC3A.SI.Rowlex.AssemblyGenerator generator; 

    private void RunAssemblyGeneration(XmlDocument ontologyFileInRdfXml) 
    { 
     this.generator = new NC3A.SI.Rowlex.AssemblyGenerator(); 
     this.generator.GenerateAsync(ontologyFileInRdfXml, "myAssemblyName", 
             null, this.OnGenerationFinished); 
    } 

    private void OnGenerationFinished(string errorMessage) 
    { 
     if (errorMessage == null) 
     { 
      // Success 
      // Displaying warnings and saving result 
      string[] warnings = this.generator.Warnings; 
      this.generator.SaveResult(@"C:\myAssemblyName.dll"); 
       // Important! One generator instance can be executed only once. 
       this.generator = null; 
       this.RejoiceOverSuccess(); 
      } 
     else 
     { 
       // Failure 
       this.MournOverFailure(); 
      } 

    } 

런타임에서 어셈블리를 생성 할 경우

, 당신이 사용자의 필요에 따라 다시 반복 반복 할 수 있음을 가정하지만, 여기에 간단한 예입니다. .NET은 어셈블리를 언로드 할 수 없기 때문에 여기를주의해야합니다. 따라서 이전 실행에서 어셈블리를 제거 할 수 없습니다. 해결책은 언로드 할 수있는 새 AppDomain에서 매번 생성 코드를 실행하는 것입니다. OwlGrinder.exe가 정확히이 작업을 수행하면 MainForm.cs 내에 피크를 나타낼 수 있습니다.

2

네, Lame, 프로그래밍 방식으로 .NET 코드를 생성 할 수 있습니다.

몇 가지 옵션이 있습니다.

  1. 코드를 텍스트로 생성하십시오.
    앱 내에서 .cs 또는 .vb 소스 파일을 컴파일 할 수 있습니다. 초보자를위한 Microsoft.CSharp.CSharpCodeProvider 클래스의 도움말을 참조하십시오. 프로그래밍 방식으로 컴파일러를 호출하고 포함 할 리소스, 생성 된 어셈블리를 배치 할 위치, 종속성 등을 지정합니다. 여기서 한 가지 시나리오는 template.cs 파일을 사용하여 조금 더 많은 코드를 포함시킨 다음 컴파일하는 것입니다. 결과는 해당 코드로 인해 어셈블리 (원하는 경우 .dll 또는 .exe 또는 .netmodule)입니다. 리플렉션을 사용하여 해당 어셈블리를로드하고 호출 할 수 있습니다.

  2. 문서 개체 모델을 사용하여 코드를 만듭니다.
    여기에 관련된 기능 영역은 "CodeDom"이라고하며, 문서 개체 모델이 .NET 코드를 만드는 데 사용된다는 점을 제외하면 웹 페이지의 HTML DOM처럼 작동합니다. 프로그래밍 방식으로 DOM 요소를 사용하여 코드를 구성합니다. 된 CodeDom 일의

예 :

var class1 = new System.CodeDom.CodeTypeDeclaration(className); 
class1.IsClass=true; 
class1.TypeAttributes = System.Reflection.TypeAttributes.Public; 
class1.Comments.Add(new System.CodeDom.CodeCommentStatement("This class has been programmatically generated")); 
// add a constructor to the class 
var ctor= new System.CodeDom.CodeConstructor(); 
ctor.Attributes = System.CodeDom.MemberAttributes.Public; 
ctor.Comments.Add(new System.CodeDom.CodeCommentStatement("the null constructor")); 
class1.Members.Add(ctor); 

// add one statement to the ctor: an assignment 
// in code it will look like; _privateField = new Foo(); 
ctor.Statements.Add(new System.CodeDom.CodeAssignStatement(new System.CodeDom.CodeVariableReferenceExpression("_privateField"), new System.CodeDom.CodeObjectCreateExpression(fooType))); 


// include a private field into the class 
System.CodeDom.CodeMemberField field1; 
field1= new System.CodeDom.CodeMemberField(); 
field1.Attributes = System.CodeDom.MemberAttributes.Private; 
field1.Name= "_privateField"; 
field1.Type=new System.CodeDom.CodeTypeReference(fooType); 
class1.Members.Add(field1); 

등 등 당신은 너무 정기적 방법, 코드에 문 모든 종류의 등을 추가 할 수 있습니다. CodeDom은 언어가 지원하는 모든 것을 지원합니다. lambdas 및 linq 표현식, 조건부 및 제어 흐름 등을 수행 할 수 있습니다.

그런 다음 해당 클래스를 컴파일하고 디스크에 저장하거나 메모리에 저장하고 동적으로로드 할 수있는 어셈블리를 다시 생성 할 수 있습니다.

관련 문제