ExcUsr2008
asked on
C# DynamicMethod
I'm trying to set SqlParameters dynamically by mapping them to the respective parameters of an object which will be known during the runtime.
I just need to set its value if the object property is not NULL. Something like below:
if(!string.IsNullOrEmpty( Person.Name){
command.Parameters[0].Valu e = Person.Name
}
I'm wondering if someone could help me with the IL code/DynamicMethod to accomplish this.
Thanks!
I just need to set its value if the object property is not NULL. Something like below:
if(!string.IsNullOrEmpty( Person.Name){
command.Parameters[0].Valu
}
I'm wondering if someone could help me with the IL code/DynamicMethod to accomplish this.
Thanks!
Person person = GetPerson();
SqlCommand command = GetSqlCommand();
...
for (int i = 0; i < command.Parameters.Count; i++)
{
System.Reflection.PropertyInfo propertyInfo =
typeof(person).GetProperty(command.Parameters[i].ParameterName.Substring(1));
//IL Code ...
}
Are your property names same as the command parameter names?
ASKER
Of course, Yes.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks for your response. I could use reflection but my understanding is that reflection could take a hit on the performance hence i thought of emitting the dyn code to see if that helps.
ASKER
I'm opting reflection method as I've not found solution on DynamicMethod using IL yet. I however award you the points for your response.
ASKER
Helped partially