Life, Surf, Code and everything in between
WebLog Posts in Category CSharp
A Small Utility to Delete Files recursively by Date
November 30, 2012 @ 9:13pm
After again searching for a script or tool that can easily delete files recursively down a folder hierarchy with a date filter and coming up with several 'almost there' solutions I sat down and created a small Console app that handles this task. I've posted the resulting project on Github, with both the binary and source code, in case you ever find yourself with the same need.
Back to Basics: When does a .NET Assembly Dependency get loaded
November 03, 2012 @ 4:39pm
Assembly loading in .NET is often a cause of confusion. So many times I've heard how evil it is to add a reference to some big assembly, if it's just a minor feature. But .NET is really smart in assembly loading and by default uses just in time loading of referenced assemblies. In this post I review when assemblies are loaded with a few simple examples that demonstrate the process.
Dynamic Code for type casting Generic Types 'generically' in C#
October 23, 2012 @ 2:06am
Here's a short entry on how dynamic can be a life saver when you can't cast a type between multiple objects, specifically when the object in question is a generic type without a common base type to cast to.
Amazon Product Advertising API SOAP Namespace Changes
May 03, 2012 @ 4:36am
Amazon recently broke their Product Advertising API by rolling out a new service version at the same URLs of the old service, which resulted in immediate failure of service. It's an easy fix once you know where to look. Here's how.
Creating a dynamic, extensible C# Expando Object
February 08, 2012 @ 2:28am
In this post I discuss a custom extensible dynamic type implementation that allows you to extend existing types with dynamic properties at runtime.
Dynamic Types and DynamicObject References in C#
February 01, 2012 @ 2:47am
This post explains the difference between dynamic reference and explicit references of an IDynamicObject implementation and how they behave differently.
Creating a Dynamic DataReader for easier Property Access
December 06, 2011 @ 5:04am
Custom dynamic types in .NET are great to wrap other data structures into easier to use and cleaner object.property interfaces. In this post I demonstrate how you can create a dynamic DataReader that allows access to a DataReader's fields using plain object.property syntax.
Creating a Dynamic DataRow for easier DataRow Syntax
November 24, 2011 @ 3:13am
The Dynamic Language Runtime features in .NET 4.0 make it very easy to create custom dynamic types that use alternate 'data sources' to expose a new member interface. In this post I describe a simple example that exposes a classic DataRow as a dynamic type to allow cleaner syntax and no need for type casting when accessing DataRow objects.
Dynamically creating a Generic Type at Runtime
November 11, 2011 @ 4:39am
How do you get a type reference to a generic type instance for a specific set of generic type parameters at runtime? Generics is primarily a coding time tool that creates code at compile time. It's not real easy to create generic types at runtime using dynamic type information. In this post I talk about an issue I ran into while trying to create dictionary types on the fly.
An Xml Serializable PropertyBag Dictionary Class for .NET
September 27, 2011 @ 4:50am
.NET includes many Dictionary related types but serialization of Dictionaries leaves a lot to be desired. In this post I show how to create a serializable PropertyBag class that makes it easy to XmlSerialize arbitrary key/value pairs into XML and back.
Translating with Google Translate without API and C# Code
August 06, 2011 @ 3:44pm
Google Translate recently changed over to an AJAX UI that broke some of my old screen scraping code I used to do translations in my ASP.NET Resource Provider resource administration app. After a bit of spelunking I found an alternative using the REST calls directly to get back the functionality I lost.
Getting the innermost .NET Exception
July 01, 2011 @ 12:32am
In some cases when exceptions are thrown, the innermost exception is what holds the most important information. For example, dynamic method calls via Reflection or dynamic invocation. Here's a simple funciont that is useful for easily retrieving the innermost exception.
HttpWebRequest and Ignoring SSL Certificate Errors
February 11, 2011 @ 5:14am
Man I can't believe this. I'm still mucking around with OFX servers and it drives me absolutely crazy how some these servers are just so unbelievably misconfigured. I've recently hit three different 3 major brokerages which fail HTTP validation with bad or corrupt certificates at least according to...
Finding a Relative Path in .NET
December 20, 2010 @ 6:17pm
Here’s a nice and simple path utility that I’ve needed in a number of applications: I need to find a relative path based on a base path. So if I’m working in a folder called c:\temp\templates\ and I want to find a relative path for c:\temp\templates\subdir\test.txt I want to receive back subdir\test.txt. Or if I pass c:\ I want to get back ..\..\ – in other words always return a non-hardcoded...
The dynamic Type in C# Simplifies COM Member Access from Visual FoxPro
September 13, 2010 @ 4:25pm
The addition of the new dynamic typing in the .NET runtime greatly simplifies COM Interop when passing FoxPro objects to .NET. Dynamic types remove the need to use explicit Reflection on dynamically generated FoxPro types that are returned over COM interop and so reduce code complexity and produce more natural and readable code removing one of the annoyances in FoxPro and .NET COM Interop.
.NET WebRequest.PreAuthenticate – not quite what it sounds like
February 18, 2010 @ 1:38pm
On a few occasions I've dealt with Web Services that use - yuk - Basic Authentication and require pre-authentication on the very first request to the server with the server first sending a challenge. This is unusal for HTTP authentication which typically requires a challenge first and then a response with the auth information in the header. The latter approach is what the .NET client components produce by default. PreAuthenticate unfortunately is not quite true to its name and in order to provide true pre-authentication on the first request manual header manipulation is required.
LINQ to SQL, Lazy Loading and Prefetching
October 12, 2009 @ 2:56am
Lazy loading in an ORM can be really useful or a royal pain if you walk through a lot of data. LINQ to SQL uses lazy loading of related entities and entity sets but there are a couple of ways that allow you to do eager loading instead. Both require some extra effort and foresight.
Fun with Func<T,TResult> Delegates, Events and Async Operations
October 05, 2009 @ 1:38am
Delegates are powerful for event handling, highly useful in LINQ and a core requirement for async operations. Func
makes using delegate based logic a lot easier by providing a generic implemenation that in many cases allows you to skip creation of a separate delegate and instead just point at the Func
definition which can be a big timesaver and a nice way to remove delegate defintions from your namespaces.
Lookbehind in Regex searches
October 03, 2009 @ 2:03am
Regex's more esoteric features are easy to miss as they make sense to me only in a context that applies to my work. I've certainly read about lookahead and lookbehind before but until I needed it today and was pointed to look at this functionality in the Regex engine it just didn't sink in. Lookbehind allows matching or not matching of a string before the the string you are interested in. It effectively allows you to look for values that don't have x before another match which is actually quite common.
Generic Types and Inheritance
August 18, 2009 @ 11:20am
Although I use Generics extensively, every once in a while it still throws me for a loop when dealing with complex generic parameters and inheritance. In this post I talk about a compilation error I ran into when trying to inherit a generic type including its generic parameters and a way to get around this particular issue.
Simplistic Object Copying in .NET
August 04, 2009 @ 8:04am
Copying object data between instances of objects is something that needs to be done quite frequently yet there are few tools that perform this task well. Here is an admittedly simplistic routine that I nevertheless find useful in many situations.
Dynamic Delegates with Expression Trees
March 08, 2009 @ 5:06am
Nate Kohari posted a nice example on how to create a dynamic delegate implementation on the fly using LINQ Expression Trees. I have lots of dynamic code I run in applications so I experimented around with this a bit and compared it to other more traditional approaches with some interesting results.
"Variable is assigned but its Value is never used" Warning
February 28, 2009 @ 2:42am
It’s late, but here’s a little humor for ‘ya. I’ve been noticing recently that I’ve been getting some odd warnings in Visual Studio for variables that are defined and never used. Check out the following screen shot from C# code editor: Notice how the compiler is warning about isSelected not being used, but as you can see just a few lines down it is in fact being used in an assignment. At first I...
Using Enums in List Controls
February 20, 2009 @ 12:00am
Using enums for display value in applications can be nice and easy with a little bit of help. While enums generally shouldn't be treated as data, in some situations it might just be more convenient to use enum definitions rather than database look up tables for data that is defined in the object/domain model. Here are a few ways you can easily use these enum values.
December 19, 2008 @ 2:51am
One thing I do quite frequently in applications is parsing strings and a common scenario requires extracting a string from within another string with delimiters. Here's a small utility function out of my StringUtils library that provides this functionality easily.
Accessing a SafeArray Result from a COM Call in C#
August 26, 2008 @ 6:18am
I'm calling a COM object from managed code that's returning a binary response, which is returned as a SafeArray of bytes from the COM server. The problem is the SafeArray is not exactly easily accessed in .NET and the debugger provides some misleading information for the returned COM type. Here's how to access the SafeArray from C# code.
Static Singletons for ASP.NET Controls
July 11, 2008 @ 12:19am
When building generic ASP.NET Server controls that also provide a sort of API service to other custom controls or page level code, it's often necessary to ensure that only a single instance of a control exists, and that only that single instance of this control or component can be accessed in the context of an ASP.NET request. Using HttpContext and it's Item collection makes it easy to create reusable, cacheable instances and ensure you're only running a Singleton instance of it.
Variable Scoping in Anonymous Delegates in C#
April 26, 2008 @ 6:24pm
LINQPad as a Code Snippet Execution Engine
April 17, 2008 @ 12:45am
LINQPad is an awesome tool to quickly execute code. Besides the obvious LINQ testing features that its name implies, LINQPad also can come in quite handy as a generic .NET code snippet execution utility that allows you to run any .NET expression or single statement block. It's a great tool for your toolbox to quickly check behavior of BCL functions for example...
Book Review: LINQ in Action
March 21, 2008 @ 5:36am
Just got done reading LINQ in Action and it's an excellent read. Rarely do I read books cover to cover, but this book definitely made me want to go through the whole thing as it's full of little gems of information, things I didn't know and lots of new ideas I can hopefully apply soon.
Odd string.Replace Chaining Behavior
March 05, 2008 @ 2:05am
I ran into a bit of a head scratcher today with a routine that does some string manipulation. It's an old routine that I use to help me do the equivalent of ResolveUrl() outside of the context of the ASP.NET Page framework - typically in static code somewhere or as part of a handler or other component. The process is easy enough but I ran into a snag with the special case of resolving a root web...
Chaining the C# ?? Operator
January 23, 2008 @ 6:12pm
The C# 2.0 ?? operator makes it easy to assign a default value to nulls. Nothing new here, but did you know that you can also chain the operator to quickly check many value and find the first non-null vlaue?
Doing away with an Assembly Dependency
January 12, 2008 @ 6:37pm
I'm looking at some oldish code that sits inside of a web control library. This is my main web control library were I keep all of my custom controls that I typically use. It includes all the AJAX controls, a custom databinder, some generic status display controls, a number of stock control...
Accepting invalid Certificates for WCF/Web Services/HttpWebRequest gets easier
December 12, 2007 @ 2:37am
Looks like .NET 3.0/3.5 has updated functionality to allow setting certificate certificate polices. In fact I noticed that the old mechanism I showed in an earlier post has been marked as obsolete, but there's a nicer replacement mechanism available now. I frequently use Certificate policy to...
Detecting Text Encoding for StreamReader
November 28, 2007 @ 1:04am
Reading Text files with proper encoding and byte order marks can be a bit of a pain when using a StreamReader as there's no detection of no byte order mark that defaults to UTF-8 which is usually incorrect. Here are a few thoughts on explicitly detecting BOM settings and getting a corresponding Encoding.
Anonymous Types in C# 3.0
November 15, 2007 @ 4:25pm
One of the most convenient features of C# 3.0 is the ability to create new types 'on the fly' using Anonymous Types. Anonymous Types are essentially compiler generated types that you don't explicitly declare with an official type declaration. Rather you define the type inline as part of the code...
Type Intializers in C# 3.0
November 12, 2007 @ 1:20am
During my LINQ to SQL session at DevConnections somebody in the audience asked what happens behind the scenes when you use type initializers in C# 3.0 and well it turns out I gave a hedged answer with a guess that was - uhm - wrong. So here's a discussion of how this feature actually works....
A few C# Time Utilities for Fractional Time Values
September 24, 2007 @ 11:34pm
I found myself working with displaying time values a bit today for a demo app I'm building that needs to display elapsed time values in a meaningful way. There's display, but also storage of total values in a rounded fashion. Here are couple of routines that helped make short work of the time values.
Reflection, GetMember() and COM Objects?
September 17, 2007 @ 12:32am
I'm working on an old app that interfaces with a legacy COM object. In reviewing some of my wwDataBinder code I noticed that it didn't work against COM objects for databinding. With a few minor changes I've been able to make the binding code work by using the the higher level Type.InvokeMember...
Dynamic Type Invokation in .NET
August 30, 2007 @ 4:01am
There many ways to dynamically instantiating types in .NET and while it's not a common task when you need it you might find that there are a lot of different ways that you can instanatiate a type. Here's some discussion of different ways you can do it and a few helper functions to make life a bit easier.
How many ways to do a String Replace?
April 30, 2007 @ 1:58am
You ever pull this stunt? You're in the middle of working and you realize that you need a string function. .NET makes string manipulation fairly easy but it can often be frustrating to find just the right function. The issue is that .NET string functions are scattered over several different objects....
Blocking IIS IP Addresses with ASP.NET
April 28, 2007 @ 7:37pm
Over the last few months I've had increasing SPAM traffic coming to my site, usually from a few select IP address groups. Most of this site spamming crap that I actually catch tries to simply POST to every possible URL on the site apparently that contains a FORM tag, spewing forth a plethora of...
Rounding down a Date in SQL
November 15, 2006 @ 2:36pm
Dates in .NET are always represented as DateTime values which include a time portion. Sometimes I need to use dates that are ‘just’ date values and there’s no native mechanism to extract just the date as far as I can see.
August, 2014 (1)
July, 2014 (2)
June, 2014 (4)
May, 2014 (3)
April, 2014 (1)
March, 2014 (2)
February, 2014 (2)
January, 2014 (3)
December, 2013 (4)
November, 2013 (5)
October, 2013 (4)
September, 2013 (3)
August, 2013 (2)
July, 2013 (3)
June, 2013 (4)
May, 2013 (2)
April, 2013 (5)
March, 2013 (4)
February, 2013 (2)
December, 2012 (2)
November, 2012 (7)
October, 2012 (3)
September, 2012 (3)
August, 2012 (5)
Visual Studio (37)
Web Services (17)
Web Api (16)
Microsoft AJAX (8)
Sql Server (7)
Web Connection (7)
West Wind Ajax Toolkit (7)
Live Writer (5)
Html Help Builder (5)
Visual Studio (4)
Entity Framework (4)
Source Control (4)
Software Development (3)
Help Builder (3)
Internet Explorer (2)
Web Deployment Projects (1)
Web Design (1)
Control Development (1)
Dynamic Types (1)
© Rick Strahl, West Wind Technologies, 2005 - 2014