Link to home
Start Free TrialLog in
Avatar of ExcUsr2008
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].Value = Person.Name
}

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 ...

            }

Open in new window

Avatar of Ravi Vaddadi
Ravi Vaddadi
Flag of United States of America image

Are your property names same as the command parameter names?
Avatar of ExcUsr2008
ExcUsr2008

ASKER

Of course, Yes.
ASKER CERTIFIED SOLUTION
Avatar of Easwaran Paramasivam
Easwaran Paramasivam
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

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.
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.
Helped partially