?
Solved

C# DynamicMethod

Posted on 2011-10-12
6
Medium Priority
?
346 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:ExcUsr2008
  • 4
6 Comments
 
LVL 16

Expert Comment

by:SriVaddadi
ID: 36959819
Are your property names same as the command parameter names?
0
 

Author Comment

by:ExcUsr2008
ID: 36959844
Of course, Yes.
0
 
LVL 16

Accepted Solution

by:
Easwaran Paramasivam earned 1500 total points
ID: 36960154
You can get the properties name and value using reflection and you can apply them in your Command property appropriatly.  Below code can help you.
using System;
using System.Collections;
using System.Linq;
using System.IO;
using System.Reflection;

namespace MyNamespace
{

    class MyClass
    {
        private int myProperty;
        // Declare MyProperty.
        public int MyProperty
        {
            get
            {
                return myProperty;
            }
            set
            {
                myProperty = value;
            }
        }
    }


    class Program
    {
        public static void Main(string[] args)
        {
            
            try
            {
                // Get the Type object corresponding to MyClass.

                MyClass objMyClass = new MyClass();
                objMyClass.MyProperty = 5;
                Type myType = typeof(MyClass);
                // Get the PropertyInfo object by passing the property name.
                PropertyInfo myPropInfo = myType.GetProperty("MyProperty");
                // Display the property name.
                Console.WriteLine("The {0} property exists in MyClass.", myPropInfo.Name);
                Console.WriteLine("The {0} property value is {1}", myPropInfo.Name, myPropInfo.GetValue(objMyClass,null).ToString());
            }
            catch (NullReferenceException e)
            {
                Console.WriteLine("The property does not exist in MyClass." + e.Message);
            }

            Console.ReadKey();
        }
    }

   
}

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ExcUsr2008
ID: 36961893

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

Author Comment

by:ExcUsr2008
ID: 36989874
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.
0
 

Author Closing Comment

by:ExcUsr2008
ID: 36989877
Helped partially
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month16 days, 5 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question