Convert an object to string in .net
A while back I needed a way to take an object in C# and convert it to a string, this made it easy for loggin in case of errors I knew what was sent and what was received back. I wanted to set something up so I could recursively iterate over the object and pull out and child objects in arrays and such. It took a little bit of work to write up but I was finally able to accomplish this. Here is what I came up with. If you have any suggestions on improving this let me know.
public static string ConvertObjectToString(object Obj)
{
string sResult = "";
if (Obj != null)
{
Type objType;
PropertyInfo[] colPropertyInfo;
objType = Obj.GetType();
colPropertyInfo = objType.GetProperties();
//I don’t know how to get a generic list to be recursive so I hard coded the name “List`1″ hopefuly this is true
// maybe switch to something like: (objPropertyInfo.PropertyType != typeof(string) && !objPropertyInfo.PropertyType.IsPrimitive)
if (objType.Name == “List`1″ || objType.BaseType == typeof(System.Array))
{
foreach (object tempVar in Obj as IEnumerable)
{
sResult += ConvertObjectToString(tempVar);
}
}
else
{
foreach (PropertyInfo objPropertyInfo in colPropertyInfo)
{
if (objPropertyInfo.GetValue(Obj, null) != null)
{
sResult += objPropertyInfo.Name + “=” + objPropertyInfo.GetValue(Obj, null).ToString();
sResult += ”
“;
}
if (objPropertyInfo.PropertyType.BaseType == typeof(System.Array) || objPropertyInfo.PropertyType.Namespace == “System.Collections.Generic” || (objPropertyInfo.PropertyType != typeof(string) && !objPropertyInfo.PropertyType.IsPrimitive))
{
sResult += ConvertObjectToString(objPropertyInfo.GetValue(Obj, null));
}
}
}
}
return sResult;
}
I have some coments on where I hard coded some values. If there is a different way of doing this it would be greatly appreciated.
Published by admin on June 26th, 2009 | Filed under Uncategorized | Comment now »The GridView Row Drag Overlay Extender (.net 3.5)
NEW WEEZER VIDEO
There is a new weezer video out and I am really excited about. The album does not come out until June 3 (which I will buy). I have been a big weezer fan ever since high school and this song reminds me of some of the older things they have done. I was also impressed by all the cameo’s in this video including K-Fed.
If you are looking for something and want a smile on your face check it out. The name of the song is Pork and Beans and will be featured on an upcomming self titled album.
Published by nateastle on May 23rd, 2008 | Filed under Uncategorized | Comment now »
Driving is a luxury
Many people have been complaining about gas prices soaring through the roof and want the government to step in. Driving your car really isn’t a necessity as more of a luxury. If you do not like the price of gas cut back the driving you do. There are several things you can do to help cut the cost.
Cut back on the electricity you use. There are several power plants that use fossil fuel for your home. If you can cut back on the amount of electricity you use then you will do your part.
Cut back on your driving. You can start to ride your bike to work. If you live further away than biking then drive part way to work and take your bike the rest of the way.
I would say use the public transportation system but that is a huge gas hog with not a lot of use. It is hugely subsidized by the government with little return on it.
Don’t rely on the government to do anything for you. No matter what the government says they are going to do it will not get done. Right now there is a republican president with a democratic congress and neither one of them are finding ideas to help with the price. A tax holiday is not the answer. It may seem like a relief but the price of oil keeps on going up which means the price of gas will keep on going up. To get rid of the tax will not help the bottom line issue of getting the oil prices back down / stabilizing the dollar.
Remember to do your part. Driving is a luxury; there are countries where less than 50% of the people own a car. Do your part to help and stop relying on others to help you out.
Published by nateastle on May 8th, 2008 | Filed under Uncategorized | Comment now »Guns vs Doctors
During this time of year you may go to your doctor for various reasons. Before you make the trip to your doctor consider the following statistics:
Doctors:
- The number of physicians in the U.S. is 700,000.
- Accidental deaths caused by Physicians per year are 120,000. (and they cause millions more with known-to-be-fatal prescription drugs and unnecessary surgeries.)
- Accidental deaths per physician is 0.171.
(Statistics courtesy of U.S. Dept of Health Human Services.)
Guns:
- The number of gun owners in the U.S. is 80,000,000.
- The number of accidental gun deaths per year, all age groups, is 1,500.
- The number of accidental deaths per gun owner is 000188.
(Statistics courtesy of F.B.I.)
Statistically, doctors are approximately 9,000 times more dangerous than gun owners.
Remember, “Guns don’t kill people, doctors do.”
FACT: Not everyone has a gun, but almost everyone has at least one doctor.
Please alert your friends to this alarming threat immediatley. We must ban doctors before this gets completely out of hand!
Note: Out of concern for the public at large, the statistics on lawyers have been withheld for fear the shock would cause people to panic and seek medical attention.
Published by nateastle on December 17th, 2007 | Filed under Uncategorized | 1 Comment »Dangers of having kids
Having kids is one of the joys / dangers in life. Last year my wife left me at home to watch the kid and while watching the kid she fell off the bed and broke her arm. I am not the type of person to stay at home and babysit or “parent” as some dad’s call it as to not sound as girly.
With that being said we are expecting our second kid here in the next few months. I am hoping that I won’t allow anything like this to happen. If it does, I am sure my wife is expecting it.
There are many dangers kids run into everyday. Make sure you are smart and know what your kids are doing. Keep track and don’t play to rough with the little ones. For some reason I can see the dad and the boy having a sword fight with forks and the boy didn’t get out of the way fast enough.
Published by nateastle on September 5th, 2007 | Filed under Uncategorized | Comment now »I believe the children are our future
Last Friday night was the Miss Teen USA pageant. For those of you who missed it, Miss South Carolina had a great response to her question.
It’s great to see where our education system is at. Granted she is on national TV and is probably nervous but she has made it through other pageants. I think her coach told her to use key words which are: “such as”, “South Africa”, and of course “Iraq” and if she could use those words regardless of the question she would win. At least she has her looks going for her.
Published by nateastle on August 27th, 2007 | Filed under Uncategorized | 1 Comment »GrandCentral
I have always been a fan of the tools Google produces. I have used everything from their search engine, maps, analytics, and even AdSense, I recently found they purchased GrandCentral.com, which is a location where you can have all of your phone numbers be tied to one.
I have wanted a service like this for a while. I have multiple phone numbers and have had a hard time keeping track of them. The best part is the service is for free, that’s right free. Like all other Google products they are for free, however, this is by invite only.
Some of the features of Grand Central are ringing all phones when a call comes in, a central voicemail box, click to call, mid call transfer (incase you do not want to use cell minutes or phone battery is dying), and many others. If you have ever needed to have a central location for your phone numbers this is the type of service you need. If you are in need of an invite, I would be willing to exchange a link on a site for an invite to GrandCentral, just leave a comment.
Published by nateastle on August 13th, 2007 | Filed under Uncategorized | Comment now »Something for nothing?
Throughout dealing with customer service I have learned a few things, if you are a polite disgruntled person, you can get something for free. A little while back I called Comcast to cancel my Internet to go through another local ISP, Comcast lowered my rate for a few months in order for me to stay on. Blockbuster will lower your monthly subscription if you try to cancel. Most places will give you something for free if you are politely disgruntled.
A little while back my wife and I went to watch a movie, the movie started late and there were a ton of commercials, my wife went onto the website to complain about it, a little while later we had free movie passes. Her meal was burnt at a restaurant and she went onto their website to complain since the waitress never came back after she dropped off the food and before long we had a free coupon for a free meal from their drestaurant.
I am not saying go to other peoples websites and complain about the quality of service, but it makes it a lot easier then complaining to the server and not knowing what you are going to be getting back to eat.
Published by nateastle on August 11th, 2007 | Filed under Uncategorized | Comment now »Freedom Fries vs French Fries
What is the big deal with wording of things? I am not one to like the french, in fact I despise the french. I think France is the United State’s Newman. You would think it would be Canada but it’s not. Canada is a close second since they have french as one of thier national language. Is it really a necessary to change the wording of something when it has been in our society for many years? This has happened time and time again. You have to be politically correct or else you can get stared down, strange looks or even fired from a job. Is this necessary for society’s to continue down this path? I say no, if something has worked for so long why change it now?
Recently there was an act, no child left behind. If a parent does not want to go through the effort with their child to help them with school work then why make the teachers do it? I call it the no child moving forward act. Our society was built on competition. You do not see everyone winning in baseball, you do not hear people settle for mediocrity so why do it now? I say honor greatness, if a child is doing better than all the other encourage learning and help them succeed.
Published by nateastle on August 8th, 2007 | Filed under Uncategorized | 1 Comment »





