String path=openFileDialog1.FileName;
System.Reflection.Assembly a = System.Reflection.Assembly.LoadFrom(@path);
Type[] arrT = a.GetTypes();
listBox1.Items.Clear();
foreach (Type t in arrT)
{
listBox1.Items.Add("Namespace = " + t.Namespace);
listBox1.Items.Add("Class names = " + t.Name);
System.Reflection.MethodInfo[] arrMI = t.GetMethods();
System.Reflection.FieldInfo[] arrFi = t.GetFields();
System.Reflection.ConstructorInfo[] arrCi = t.GetConstructors();
foreach (System.Reflection.ConstructorInfo ci in arrCi)
{
listBox1.Items.Add("Contructor = " + ci.Name);
System.Reflection.ParameterInfo[] arrPI = ci.GetParameters();
foreach (System.Reflection.ParameterInfo pi in arrPI)
{
listBox1.Items.Add("Parameter = " + pi.Name);
}
}
foreach (System.Reflection.FieldInfo fi in arrFi)
{
listBox1.Items.Add("Field = " + fi.Name);
}
foreach (System.Reflection.MethodInfo mi in arrMI)
{
listBox1.Items.Add("Method = " + mi.Name);
System.Reflection.ParameterInfo[] arrPif = mi.GetParameters();
foreach (System.Reflection.ParameterInfo pif in arrPif)
{
listBox1.Items.Add("Parameter Name = " + pif.Name);
listBox1.Items.Add("Parameter DataType = " + pif.ParameterType.Name);
}
}
}
Tuesday, November 11, 2008
Explorer Your Dll
How to invoke your Dll using relative path
public Form1()
{
InitializeComponent();
Assembly a = Assembly.LoadFile(@"F:\by Ratno\Final Skripsi\UML Studio Project\NeutralNotation\bin\Debug\NeutralNotation.dll");
Module m = a.GetModule("NeutralNotation.dll");
Type typen = null;
foreach (Type t in m.GetTypes())
{
if (t.Name.Trim().Equals("Neutral"))
{
typen = t;
}
}
ConstructorInfo[] ci;
ci = typen.GetConstructors();
d=(AbstractDiagram.IDiagram) ci[0].Invoke(null);
toolStrip1.Items.AddRange(d.GetNotationCollection());
}
Assembly Explorer
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
namespace Engine.Asm
{
///
/// Class ini digunakan untuk melakukan penjelajahan ke
/// dalam dll
///
public class AssemblyExplorer
{
private string path;
private List
protected Assembly asm;
protected Type[] arrType;
public string getName()
{
return asm.ManifestModule.Name;
}
///
/// Buat instant baru dari class AssemblyExplorer
///
/// path lokasi dll yang akan diload
public AssemblyExplorer(string path)
{
try
{
this.path = path;
asm = Assembly.LoadFile(path);
////System.Windows.Forms.MessageBox.Show(asm.ManifestModule.Name);
arrType = asm.GetTypes();
lstBaseName = new List
foreach (Type t in arrType)
{
if (!lstBaseName.Contains(t.BaseType.Name))
lstBaseName.Add(t.BaseType.Name);
}
}
catch (ReflectionTypeLoadException e)
{
System.Windows.Forms.MessageBox.Show(e.Message,e.GetType().Name);
}
catch (BadImageFormatException e)
{
System.Windows.Forms.MessageBox.Show(e.Message, e.GetType().Name);
}
catch (NullReferenceException e)
{
System.Windows.Forms.MessageBox.Show(e.Message, e.GetType().Name);
}
}
///
/// Ambil daftar kelas induk dari semua kelas
/// yang terdapat pada dll ini
///
///
public List
{
return lstBaseName;
}
///
/// Cek, apakah suatu dll memiliki kelas
/// yang memiliki suatu kelas induk
///
/// nama kelas induk
///
/// memiliki
public bool containtBaseClass(string baseClass)
{
if (lstBaseName == null)
return false;
foreach (string s in lstBaseName)
{
if (s.Trim().Equals(baseClass))
return true;
}
return false;
}
///
/// Ambil suatu class berdasar nama
///
/// nama kelas yang dicari
///
/// atau mengembalikan kelas
/// bila ditemukan
public Type getClass(string ClassName)
{
if (arrType == null)
return null;
foreach (Type t in arrType)
{
if (t.Name.Equals(ClassName))
return t;
}
return null;
}
~AssemblyExplorer()
{
path = null;
asm = null;
lstBaseName = null;
arrType = null;
}
}
}
How to Invode Dll from C# 2005
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.IO;
using System.Windows.Forms;
namespace Engine.Asm
{
///
/// Class untuk melakukan invokasi atau pemicuan suatu dll
///
///
public class AssemblyInvoke
{
private Type selected;
private ConstructorInfo[] arrCi;
private string name, extension,dir;
///
/// Constructor untuk menciptakan instace dari class ini
///
/// path dari dll yang akan diinvokasi
public AssemblyInvoke(string path)
: base(path)
{
FileInfo fif = new FileInfo(path);
name = fif.Name;
dir = fif.DirectoryName;
extension = fif.Extension;
}
///
/// Invokasi atau picu dll
///
/// Nama class yang akan diinvokasi
///
public ResultType Invoke(string ClassNameToInvoke)
{
selected = getClass(ClassNameToInvoke);
arrCi = selected.GetConstructors();
return (ResultType)arrCi[0].Invoke(new object[] { dir,name,null});
}
}
}
Monday, November 10, 2008
How to Invoke Jar File using Dynamic Path
This post describe how to invoke some jar file using dynamic path.
Read More......
How to Read Working Directory in Java
This posting is tutorial to get working directory from java application. Working directory means a directory where our java application running.