Backgroud :
I'm using FileHelper Engine ( http://www.codeproject.com/useritems/filehelpers.asp ) to import data from CSV File to MSSQL.
This Engine generate array of Strong Type dump Object to Collect data from CSV file, and then we can fill it to MSSQL. but,
The problem is the CSV File Can Growth ( Column ) Dynamically, so i need to make the dump object dynamically too.
Solution :
Create new class than responsible to produce new type......
public class CustomTypeBuilder
{
private AssemblyName mAssemblyName;
private string mTypeName;
private string[] mPropertiesName;
private ArrayList mTypeAttribute;
private ArrayList mFieldAttribute;
private ArrayList mPropertyAttribute;
private ArrayList mMethodAttribute;
public CustomTypeBuilder(string assemblyName, string typeName, string[] propertiesName)
{
mAssemblyName = new AssemblyName();
mAssemblyName.Name = assemblyName;
mTypeName = typeName;
mPropertiesName = propertiesName;
mTypeAttribute = new ArrayList();
mFieldAttribute = new ArrayList();
mPropertyAttribute = new ArrayList();
mMethodAttribute = new ArrayList();
}
public void AddAttribute(CustomAttributeType attrType, Attribute attr, object attrValue)
{
Type[] param = null;
object[] args = null;
if (attrValue == null)
{
param = new Type[]{};
args = new object[]{};
}
else
{
param = new Type[]{attrValue.GetType()};
args = new object[]{attrValue};
}
ConstructorInfo classCtorInfo = attr.GetType().GetConstructor(param);
CustomAttributeBuilder attrBuilder = new CustomAttributeBuilder(classCtorInfo,args);
switch(attrType)
{
case CustomAttributeType.Class:
mTypeAttribute.Add(attrBuilder);
break;
case CustomAttributeType.Field:
mFieldAttribute.Add(attrBuilder);
break;
case CustomAttributeType.Property:
mPropertyAttribute.Add(attrBuilder);
break;
case CustomAttributeType.Method:
mMethodAttribute.Add(attrBuilder);
break;
}
}
public Type CreateCustomType()
{
TypeBuilder newType = null;
//Parameter Validation
if (mAssemblyName == null || mTypeName == String.Empty)
throw new Exception("Missing Class Parameter, Failed to Create Type !");
//Create New Assembly & Module
try
{
AssemblyBuilder newAssembly = Thread.GetDomain().DefineDynamicAssembly(mAssemblyName,AssemblyBuilderAccess.Run);
ModuleBuilder newModule = newAssembly.DefineDynamicModule("CustomAssemblyModule");
//Create Type
newType = newModule.DefineType(mTypeName,TypeAttributes.Public);
if (mTypeAttribute.Count > 0)
{
for(int i=0;i<mTypeAttribute.Count;i++)
{
newType.SetCustomAttribute((CustomAttributeBuilder)mTypeAttribute
);
}
}
//Create Field & Properties
if (mPropertiesName != null && mPropertiesName.GetLength(0) > 0)
{
FieldBuilder newField = null;
PropertyBuilder newProperty = null;
MethodBuilder newGetMethod = null;
MethodBuilder newSetMethod = null;
ILGenerator ilGenGet = null;
ILGenerator ilGenSet = null;
string fieldName = String.Empty;
for(int i=0;i<mPropertiesName.GetLength(0);i++)
{
fieldName = "m" + mPropertiesName
;
newField = newType.DefineField(fieldName,typeof(string),FieldAttributes.Private);
if(mFieldAttribute.Count > 0)
{
for(int j=0;j<mFieldAttribute.Count;j++)
{
newField.SetCustomAttribute((CustomAttributeBuilder)mFieldAttribute[j]);
}
}
newProperty = newType.DefineProperty(mPropertiesName
,PropertyAttributes.HasDefault,
typeof(string),new Type[]{typeof(string)});
if(mPropertyAttribute.Count>0)
{
for(int j=0;j<mPropertyAttribute.Count;j++)
{
newProperty.SetCustomAttribute((CustomAttributeBuilder)mPropertyAttribute[j]);
}
}
newGetMethod = newType.DefineMethod("Get" + mPropertiesName
,MethodAttributes.Public,
typeof(string),new Type[]{});
ilGenGet = newGetMethod.GetILGenerator();
ilGenGet.Emit(OpCodes.Ldarg_0);
ilGenGet.Emit(OpCodes.Ldfld, newField);
ilGenGet.Emit(OpCodes.Ret);
newSetMethod = newType.DefineMethod("Set" + mPropertiesName
,MethodAttributes.Public,
null,new Type[]{typeof(string)});
ilGenSet = newSetMethod.GetILGenerator();
ilGenSet.Emit(OpCodes.Ldarg_0);
ilGenSet.Emit(OpCodes.Ldarg_1);
ilGenSet.Emit(OpCodes.Stfld, newField);
ilGenSet.Emit(OpCodes.Ret);
newProperty.SetGetMethod(newGetMethod);
newProperty.SetSetMethod(newSetMethod);
}
}
}
catch(Exception)
{
throw;
}
return newType.CreateType();
}
}
don't forget the enum
public enum CustomAttributeType
{
Class=0,
Field,
Property,
Method
}
Then ... Implement That class :
private void button1_Click(object sender, System.EventArgs e)
{
string assName = "NewAssembly";
string typeName = "NewType";
string[] propName = new string[5]{'P1','P2','P3','P4','P5'}
CustomTypeBuilder typeBuilder = new CustomTypeBuilder(assName,typeName,propName);
System.Type newType = typeBuilder.CreateCustomType();
object obj = Activator.CreateInstance(newType);
FileHelperEngine engine = new FileHelperEngine(obj.GetType());
object[] arrObj = engine.ReadFile(@"c:\TEMP\export.csv");
}
You also can add custom atribute to your dump class by calling AddAttribute method in CustomTypeBuilder Class.
Just simple way, you may advance the class...
Hope It Usefull.....
Today and couple days before, I have assignment from management to interview some Professional Developers who would like to join in my team.
Rata-rata, the process needs a whole days to finished. But what i've got ? Nothing. Why ? Because mostly, they expected high salary.
What I'm very concerned about is, rata-rata mereka meminta terlalu tinggi untuk competence yang mereka miliki. Let says 4 - 6 jt for level 100-200. Is that make sense ? I Think that is too high for standard company ( Not including BI or other goverment/oil Company ).
Apakah ada panduan untuk para professional, terutama IT Developer untuk mengajukan tawaran salary mereka ? Sebagai masukan mungkin dengan melibatkan parameter Competence, atau bisa ditambahkan dengan How big the company that We would like to Join.
Before I Write my first Experience, Opinion or Article, Let Me introduce My Self.
My Full Name : Virgo Eka Hartanto Soepa
Company : SCS Astragraphia Technology
Title : Business Solution consultant
I've been work for Astra Group for about 6 years. Started as Junior Programmer with VB 6. But........... Eng ing eng ... apa yang di assign pertama kali ?? Diminta untuk mempelajari suatu bahasa versi oldiest yang jalan di Dos.. apakah itu ? CLIPPER....
Since that assignment, jadilah saya programmer Clipper selama kurang lebih 3 Tahun, tanpa mengenal dunia luar .... :( .. Tahun-tahun itu mungkin lebih dkenal sebagai Era Kegelapan Visual Programming bagi saya. Tapi sebagai orang yang selalu melihat sisi baiknya aja .. :), saya banyak belajar busines di era itu.
Good Knews when SAP Coming .... Aplikasi yang saya Support direplace Total oleh SAP. Kira-kira sekitar early 2004 lah. then mulailah di Assign untuk project yang menggunakan technology NET, with VB NET Language. Mulailah selama setahun saya membuat beberapa modul dengan menggunakan VB Net, tetapi dengan pengetahuan sebatas API Standar dan Prosedural Style Programming alias konversi dari gaya DOS saya selama ini. And .. Bete datang menghampiri, karena apa ? Style programming saya begitu-gitu aja .. ngga ada indah2x nya, jauh dari jargon-jargon yang didengung-dengungkan ( Scalability, Manageability .. dll ).
Era C# datang sekitar early 2005, masih bete juga, ya karena style itu lah ... then Someone hadir di Astra .. siapa ? Agus Kurniawan .. salah satu MVP di Indonesia .. begitu sih katanya. Awalnya saya masih cuek ( ngga tau MVP itu apaan sih ? ), sampai kemudian dapat suatu project yang di Handle sama Si Boss itu. Apa yang terjadi ?? Semua code yang saya buat dengan Style saya itu di CAP BAD SMELL...
Tapi mulai detik itu juga, saya diperkenalkan dengan komunitas ini :) .. Thank's God ...
Akhirnya Era Kegelapannya berakhir, ternyata banyak sekali knowledge yang bisa dipelajari.
Saya mulai diperkenalkan OO, Technolgy dan Pattern.
Perjalanan masih jauh.. Upgrade Knowlegde & Establish Network, Friends and Partners... Thats My Motto...
Wish me Luck..
End of Introducing.