CSharpFeeds - All your C# feeds in one place.

Sponsors

Feed: i am a camera

Site: http://iamacamera.org/ Link: http://iamacamera.org/default.aspx?feed=rss20§ion=develop/code_snippets

Monday, January 15, 2007

InnerException: Reflection's Best Friend

by no.ema.il@no.ema.il via i am a camera on 1/15/2007 1:55:54 AM

Using reflection, you've carefully crafted code to extract data from an old COM object. It works great, and you feel a sense of accomplishment. But now someone is in your office saying something about your ASP.NET app that it's not working. My Code? Of course you took the time when creating the reflection call to log any problems in the called module, so you look into the application trace log and you see this quite annoying error: Exception has been thrown by the target of an invocation ...

[ read more ]

Friday, September 15, 2006

TimeSpan Best Practices

by carl camera via i am a camera on 9/15/2006 7:45:00 AM

A recent post on c# corner provides a straightforward introduction to the TimeSpan object and shows a couple innocuous examples. Further down, Ms. Choksi disclaims any further nuances such as time zone differences or daylight saving. The problem is her code is more harmful than helpful. The TimeSpan object is very tricky and everyone needs to take Daylight Saving Time into consideration. Ignoring the topic is setting folks up for trouble down the road. Time Zones Are Irrelevant Time zones ...

[ read more ]

Thursday, September 14, 2006

TimeSpan Best Practices

by carl camera via i am a camera on 9/14/2006 6:00:00 PM

A recent post on c# corner provides a straightforward introduction to the TimeSpan object and shows a couple innocuous examples. Further down, Ms. Choksi disclaims any further nuances such as time zone differences or daylight saving. The problem is her code is more harmful than helpful. The TimeSpan object is very tricky and everyone needs to take Daylight Saving Time into consideration. Ignoring the topic is setting folks up for trouble down the road. Time Zones Are Irrelevant Time zones ...

[ read more ]

Tuesday, September 05, 2006

Filetime Utility

by carl@vinebranches.com via i am a camera on 9/5/2006 7:49:00 AM

Blosxom seems to be working for me quite well now that it started accepting comments. One of the features provides convenience but also brings with it a problem. It organizes each entry by the date it is posted. Each entry is a file, so it just reads the file date. Convenient. I don't know about you, but sometimes I need to go back and adjust text or fix a spelling error. Mostly recently, I've been adjusting the framework of my setup that affects the way each entry is structured. So when I ...

[ read more ]

Filetime Utility

by carl camera via i am a camera on 9/5/2006 7:49:00 AM

Blosxom seems to be working for me quite well now that it started accepting comments. One of the features provides convenience but also brings with it a problem. It organizes each entry by the date it is posted. Each entry is a file, so it just reads the file date. Convenient. I don't know about you, but sometimes I need to go back and adjust text or fix a spelling error. Mostly recently, I've been adjusting the framework of my setup that affects the way each entry is structured. So when I ...

[ read more ]

Friday, September 01, 2006

XmlDocument to DataSet

by carl camera via i am a camera on 9/1/2006 3:22:00 PM

XmlDocument to DataSet in two lines. This is the quickest conversion I've found so far. XmlDocument xdoc = MethodReturnsXmlDocument(); // convert to DataSet DataSet ds = new DataSet(); ds.ReadXml(new XmlNodeReader(xdoc)); ...

[ read more ]

XmlDocument to DataSet

by carl@vinebranches.com via i am a camera on 9/1/2006 3:22:00 PM

XmlDocument to DataSet in two lines. This is the quickest conversion I've found so far. XmlDocument xdoc = MethodReturnsXmlDocument(); // convert to DataSet DataSet ds = new DataSet(); ds.ReadXml(new XmlNodeReader(xdoc)); ...

[ read more ]

Thursday, July 06, 2006

C# Reflection Part 4

by carl@vinebranches.com via i am a camera on 7/6/2006 1:50:00 PM

Set Properties on Reflected Object Suppose that the method that we will eventually call requires that two properties within the object be set beforehand. Why would anyone design a class this way? I can't explain it but there is code out in the wild (ahem) that might require this. If you're one of the other unfortunate ones, here's how to set properties on an object created through reflection. Setting properties through reflection requires both a Type object and an object instantiation. We c ...

[ read more ]

C# Reflection Part 3

by carl@vinebranches.com via i am a camera on 7/6/2006 1:50:00 PM

Passing Parameters Now with the basic form established, we can move to variations on the theme. First variation: pass parameters to the method we're calling. Steps 1, 2 and 3 to load the DLL assembly and create a target object remain the same as the previous article: // load the assembly Assembly assem = Assembly.LoadFrom(@"c:\path\to\file.dll"); // create a Type object Type typClsX = assem.GetType("Fully.Qual.ClsX",true); // instantiate an object of that type object oClsX = Activator.Create ...

[ read more ]

C# Reflection Part 2

by carl@vinebranches.com via i am a camera on 7/6/2006 1:50:00 PM

The first example with be the simplest and most straightforward. We'll invoke a method that takes no parameters and returns a string. What is as simple as string s = obj.Method(); takes quite a few extra steps when using reflection. Load an assembly from a known Data Link Libarary (DLL) in a known location Create a System.Type object of the class of the object you want to instantiate Create an instance of the class object Create a System.Reflection.MethodInfo object that represents the meth ...

[ read more ]

C# Reflection Part 1

by carl@vinebranches.com via i am a camera on 7/6/2006 1:50:00 PM

For those who are unfamiliar with .NET reflection, it is a whole area of computing that allows runtime binding to public classes, methods, and properties within external DLLs. Some folks refer to this as late binding. In .NET, the usual way we call a method in another project's Data Link Libarary (DLL) is by Adding a reference to the component DLL in Visual Studio Adding a #using declaration to the top of the C# file Calling the method just as you would call a local method In the example ...

[ read more ]

C# Reflection Part 4

by carl camera via i am a camera on 7/6/2006 1:50:00 PM

Set Properties on Reflected Object Suppose that the method that we will eventually call requires that two properties within the object be set beforehand. Why would anyone design a class this way? I can't explain it but there is code out in the wild (ahem) that might require this. If you're one of the other unfortunate ones, here's how to set properties on an object created through reflection. Setting properties through reflection requires both a Type object and an object instantiation. We c ...

[ read more ]

C# Reflection Part 3

by carl camera via i am a camera on 7/6/2006 1:50:00 PM

Passing Parameters Now with the basic form established, we can move to variations on the theme. First variation: pass parameters to the method we're calling. Steps 1, 2 and 3 to load the DLL assembly and create a target object remain the same as the previous article: // load the assembly Assembly assem = Assembly.LoadFrom(@"c:\path\to\file.dll"); // create a Type object Type typClsX = assem.GetType("Fully.Qual.ClsX",true); // instantiate an object of that type object oClsX = Activator.Create ...

[ read more ]

C# Reflection Part 2

by carl camera via i am a camera on 7/6/2006 1:50:00 PM

The first example with be the simplest and most straightforward. We'll invoke a method that takes no parameters and returns a string. What is as simple as string s = obj.Method(); takes quite a few extra steps when using reflection. Load an assembly from a known Data Link Libarary (DLL) in a known location Create a System.Type object of the class of the object you want to instantiate Create an instance of the class object Create a System.Reflection.MethodInfo object that represents the meth ...

[ read more ]

C# Reflection Part 1

by carl camera via i am a camera on 7/6/2006 1:50:00 PM

For those who are unfamiliar with .NET reflection, it is a whole area of computing that allows runtime binding to public classes, methods, and properties within external DLLs. Some folks refer to this as late binding. In .NET, the usual way we call a method in another project's Data Link Libarary (DLL) is by Adding a reference to the component DLL in Visual Studio Adding a #using declaration to the top of the C# file Calling the method just as you would call a local method In the example ...

[ read more ]

C# Reflection Part 6

by carl camera via i am a camera on 7/6/2006 1:49:00 PM

Passing "out" Parameters Out parameters are parameters that are expected (and in fact for C# required) to be populated by the called method. That is, the value for the out parameter is not meaningful when passed in and receives a value after the method is called. bool GetCustName(int iCust, out string strName); We must first create the object as covered in depth in Part 2: Assembly assem = Assembly.LoadFrom(@"c:\path\to\file.dll"); Type typClsX = assem.GetType("Fully.Qual.ClsX",true); o ...

[ read more ]

C# Reflection Part 5

by carl camera via i am a camera on 7/6/2006 1:49:00 PM

Passing "ref" Parameters So far we've made conventional reflection calls. Things get a bit sticky, however, when the method we want to invoke contains a ref parameter. For example, a method with the signature: bool CheckCustName(int iCust, ref string strName); Reference parameters are not copied into the receiving method, but referenced from the caller's memory area. Creating the object is not a problem if you've been reading this series: This part was covered in depth in Part 2: Assembl ...

[ read more ]

C# Reflection Part 6

by carl@vinebranches.com via i am a camera on 7/6/2006 1:49:00 PM

Passing "out" Parameters Out parameters are parameters that are expected (and in fact for C# required) to be populated by the called method. That is, the value for the out parameter is not meaningful when passed in and receives a value after the method is called. bool GetCustName(int iCust, out string strName); We must first create the object as covered in depth in Part 2: Assembly assem = Assembly.LoadFrom(@"c:\path\to\file.dll"); Type typClsX = assem.GetType("Fully.Qual.ClsX",true); o ...

[ read more ]

C# Reflection Part 5

by carl@vinebranches.com via i am a camera on 7/6/2006 1:49:00 PM

Passing "ref" Parameters So far we've made conventional reflection calls. Things get a bit sticky, however, when the method we want to invoke contains a ref parameter. For example, a method with the signature: bool CheckCustName(int iCust, ref string strName); Reference parameters are not copied into the receiving method, but referenced from the caller's memory area. Creating the object is not a problem if you've been reading this series: This part was covered in depth in Part 2: Assembl ...

[ read more ]

C# Reflection Part 7

by carl@vinebranches.com via i am a camera on 7/6/2006 1:48:00 PM

Dealing with Remote Objects In this final installment of C# Reflection, I provide my solutions to a couple problems I encountered after successfully retrieving the remote object I wanted. In the first six installments, the object returned was always an intrinsic type such as string. The Invoke method always returns an object of type object and I've always shown simple casting as the final step. string strCustName = (string) miCusNm.Invoke(oClsX,arrParms); Remote Object Now suppose we wa ...

[ read more ]

C# Reflection Part 7

by carl camera via i am a camera on 7/6/2006 1:48:00 PM

Dealing with Remote Objects In this final installment of C# Reflection, I provide my solutions to a couple problems I encountered after successfully retrieving the remote object I wanted. In the first six installments, the object returned was always an intrinsic type such as string. The Invoke method always returns an object of type object and I've always shown simple casting as the final step. string strCustName = (string) miCusNm.Invoke(oClsX,arrParms); Remote Object Now suppose we wa ...

[ read more ]

Wednesday, May 03, 2006

Poor Man's Logger

by carl camera via i am a camera on 5/3/2006 5:22:00 PM

I saw a coworker use this technique the other day and thought it clever. I'm sure he's not the first to use it, but it can come in handy especially in static functions without a formal logger. This C# code will generate null files in your "temp" directory. The filenames will be sequenced based on a timestamp at the front, then the information you're tracking behind. The Regex.Replace is necessary to weed out the characters that Windows does not allow in filenames, and the length check keeps ...

[ read more ]

Poor Man's Logger

by carl@vinebranches.com via i am a camera on 5/3/2006 5:22:00 PM

I saw a coworker use this technique the other day and thought it clever. I'm sure he's not the first to use it, but it can come in handy especially in static functions without a formal logger. This C# code will generate null files in your "temp" directory. The filenames will be sequenced based on a timestamp at the front, then the information you're tracking behind. The Regex.Replace is necessary to weed out the characters that Windows does not allow in filenames, and the length check keeps ...

[ read more ]

Subscribe

New Feed

Product Spotlight

Recently Updated Sources

Legal Note

The content of the postings is owned by the respective author. CSharpFeeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on CSharpFeeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.

Advertise with us