• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 438
  • Last Modified:

Reflection to build SqlParameters

I am using Reflection to create lots of SqlParameters in an ASP.NET C# web application; is this a good idea, or are there some serious problems I need to be aware of?

Thanks.
0
userTester
Asked:
userTester
  • 4
  • 3
2 Solutions
 
anarki_jimbelCommented:
It's hard to say without knowing details. However, taking into account that reflection is pretty time consuming process your application performance may become quite poor. If you have many requests it can be a big problem, I think.

Read the article, for example:

http://msdn.microsoft.com/en-us/magazine/cc163759.aspx
0
 
userTesterAuthor Commented:
Thanks for that; the articles go through a bit more than what I am doing, and it seems that my situation is okay. Here's the piece that I use to get the object properties:

List<KeyValuePair<string, Type>> pType = new List<KeyValuePair<string, Type>>();

// Add object Properties to KeyValue List
foreach (PropertyInfo pInfo in type.GetProperties())
{
  pType.Add(new KeyValuePair<string, Type>(pInfo.Name, pInfo.PropertyType));
}

and

List<KeyValuePair<string, object>> pVal = new List<KeyValuePair<string, object>>();

// Get object type
Type objType = obj.GetType();

// Add Property Name and Value to return list
foreach (PropertyInfo pInfo2 in objType.GetProperties())
{
  pVal.Add(new KeyValuePair<string, object>(pInfo2.Name,propInfo2.GetValue(obj, null)));
}
0
 
anarki_jimbelCommented:
Looks not too bad :).
If you'll have any doubts about performance in future I'd recommend to do performance measurements using some profiler.
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
userTesterAuthor Commented:
Yes, the whole CreateParameters method I have put together works very well, but it's good to know that there is the performance hit that goes with it, so I will have to test that, and might have to simply hard-code the parameters for each situation.

Was hoping to avoid all the hard-coding? I wonder whether there's another more acceptable way of automating the creation of SqlParameters??
0
 
userTesterAuthor Commented:
Perhaps I should phrase the question a bit differently:

Aside from using Reflection, is there another commonly used way to automate Parameter creation?
0
 
anarki_jimbelCommented:
Not sure. May be some config file... But I'd rather use reflection.
0
 
userTesterAuthor Commented:
Thanks for answering my questions.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now