Rick Strahl's Web Log

Wind, waves, code and everything in between...
ASP.NET • C# • HTML5 • JavaScript • AngularJs
Contact   •   Articles   •   Products   •   Support   •   Search
Ad-free experience sponsored by:
ASPOSE - 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

ASP.NET (235) .NET (118) JavaScript (71) jQuery (61) ASP.NET (59) Windows (49) AJAX (47) Visual Studio (44) CSharp (43) IIS (41) HTML (35) Localization (31) LINQ (30) Personal (30) WCF (28) MVC (25) HTML5 (23) FoxPro (21) WPF (19) Web Services (19) Security (18) CSS (18) Web Api (16) COM (15) ASP.NET Core (14) IIS7 (14) ADO.NET (13) C# (13) Angular (12) Vista (12) XML (10) HTTP (10) Microsoft AJAX (8) Sql Server (8) West Wind Ajax Toolkit (7) Web Connection (7) Entity Framework (6) Html Help Builder (5) OWIN (5) Live Writer (5) ASPNET5 (5) C++ (5) SignalR (5) Silverlight (5) Source Control (4) Westwind.Globalization (4) Visual Studio (4) Mobile (4) Cordova (4) Internet Explorer (4) ISV (4) Help Builder (3) IIS7 (3) Opinion (3) Networking (3) NuGet (3) LetsEncrypt (3) ADO.NET (3) Conferences (3) DataBinding (3) WebLog (3) Software Development (3) Razor (3) RegEx (2) RSS (2) Speaking (2) Tools (2) WinForms (2) Bugs (2) ASP.NET vNext (2) Angular (2) Windows (2) WinForms (2) .NET (2) IIS (2) Office (2) Installation (2) Help (2) FireFox (1) Flexbox (1) Graphics (1) Hardware (1) IOS (1) iPhone (1) Musings (1) Markdown Monster (1) Maui (1) Linux (1) JSON (1) Addins (1) WPF (1) FoxPro (1) Internet Explorer (1) Linux (1) Markdown Monster (1) Typescript (1) Azure (1) Deployment (1) Dynamic Types (1) Control Development (1) Chocolatey (1) Windsurfing (1) wwHoverPanel (1) Travel (1) Web Deployment Projects (1) Web Design (1) Testing (1) Threading (1) Web Browser Control (1) SEO (1)

A Small Utility to Delete Files recursively by Date

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

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#

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

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

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#

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

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

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

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

.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

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

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

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

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

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

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

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

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

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

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

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

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

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

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.

String Extraction

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#

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

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#

On a few occasions anonymous methods still throw me for a conceptual loop. I'm comfortable with them in JavaScript, but in C# the behavior is a bit foreign and makes for some interesting compiler gymnastics. Anonymous methods work like closures and so variable scoping can be extended into these anonymous methods from the calling method scope which is pretty damn useful and the basis for what makes Lambda expressions work in the first place.

LINQPad as a Code Snippet Execution Engine

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

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

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

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

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

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

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

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

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

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?

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

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?

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

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

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.
West Wind  © Rick Strahl, West Wind Technologies, 2005 - 2017