How to store a collection of custom objects to an user.config file?

The way to add custom config (if you require more than just simple types) is to use a ConfigurationSection, within that for the schema you defined you need a ConfigurationElementCollection (set as default collection with no name), which contains a ConfigurationElement, as follows: public class UserElement : ConfigurationElement { [ConfigurationProperty( “firstName”, IsRequired = true )] … Read more

Templates polymorphism

I think the exact terminology for what you need is “template covariance”, meaning that if B inherits from A, then somehow T<B> inherits from T<A>. This is not the case in C++, nor it is with Java and C# generics*. There is a good reason to avoid template covariance: this will simply remove all type … Read more

Why can a .NET delegate not be declared static?

Try this: public delegate void MoveDelegate(object o); public static MoveDelegate MoveMethod; So the method-variable can be defined static. The keyword static has no meaning for the delegate definition, just like enum or const definitions. An example of how to assign the static method-field: public class A { public delegate void MoveDelegate(object o); public static MoveDelegate … Read more

How do I get the Controller and Action names from the Referrer Uri?

I think this should do the trick: // Split the url to url + query string var fullUrl = Request.UrlReferrer.ToString(); var questionMarkIndex = fullUrl.IndexOf(‘?’); string queryString = null; string url = fullUrl; if (questionMarkIndex != -1) // There is a QueryString { url = fullUrl.Substring(0, questionMarkIndex); queryString = fullUrl.Substring(questionMarkIndex + 1); } // Arranges var … Read more

Determine the file type using C#

I’ve just converted my VB.NET class to C# to detect mime types. They will be identified by a mix of sniffing the first 256 Bytes of a file(urlmon.dll – f.e. used by internet explorer) and a set of known types. You can also use it to check if the file is what it pretends to … Read more