Rick Strahl's Web Log
Wind, waves, code and everything in between...
Ad-free experience sponsored by:
- the market leader of .NET and Java APIs for file formats – natively work with DOCX, XLSX, PPT, PDF, images and more
WebLog Posts in Category CSharp
April, 2019 (3)
March, 2019 (1)
February, 2019 (4)
January, 2019 (3)
December, 2018 (2)
November, 2018 (3)
October, 2018 (2)
September, 2018 (1)
August, 2018 (2)
July, 2018 (1)
June, 2018 (3)
May, 2018 (2)
April, 2018 (2)
March, 2018 (1)
February, 2018 (1)
January, 2018 (1)
December, 2017 (2)
November, 2017 (4)
October, 2017 (2)
September, 2017 (6)
August, 2017 (1)
July, 2017 (3)
June, 2017 (2)
May, 2017 (2)
Visual Studio (50)
ASP.NET Core (33)
Web Services (19)
Web Api (16)
.NET Core (13)
Sql Server (9)
Microsoft AJAX (8)
Web Connection (7)
West Wind Ajax Toolkit (7)
Entity Framework (6)
Internet Explorer (6)
Html Help Builder (5)
Live Writer (5)
Source Control (4)
Markdown Monster (4)
Help Builder (3)
Web Browser Control (3)
Software Development (3)
Visual Studio Code (2)
ASP.NET vNext (2)
.NET Standard (2)
Visual Studio (1)
ASP.NET Markdown (1)
Dynamic Types (1)
Control Development (1)
Credit Card Processing (1)
VS Code (1)
Web Deployment Projects (1)
Web Design (1)
Web Assembly (1)
A Small Utility to Delete Files recursively by Date
November 30, 2012 - Maui, Hawaii
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 - Maui, Hawaii
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 - Maui, Hawaii
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 - Maui, Hawaii
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 - Maui, Hawaii
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 - Maui, Hawaii
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 - Maui, Hawaii
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 - Maui, Hawaii
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 - Maui, Hawaii
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 - Hood River, Oregon
.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 - Lago di Garda, Italy
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 - Hood River, Oregon
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 - Chennai, Inida
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 - Maui, Hawaii
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 - Maui, Hawaii
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 - Maui, Hawaii
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 - Maui, Hawaii
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 - Maui, Hawaii
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 - Hood River, Oregon
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 - Berlin, Germany
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 - Saalfelden, Austria
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 - Maui, Hawaii
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 - Maui, Hawaii
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 - Maui, Hawaii
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 - Maui, Hawaii
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 - Maui, Hawaii
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 - Garda See, Italy
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 - Hood River, Oregon
LINQPad as a Code Snippet Execution Engine
April 17, 2008 - Redmond, Washington
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 - Maui, Hawaii
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 - Maui, Hawaii
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 - Maui, Hawaii
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 - Maui, Hawaii
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 - Maui, Hawaii
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 - Maui, Hawaii
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 - Maui, Hawaii
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 - Maui, Hawaii
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 - Hood River, Oregon
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 - Hood River, Oregon
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 - Hood River, Oregon
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 - Maui, Hawaii
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 - Maui, Hawaii
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 - Maui, Hawaii
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.