Posts

Showing posts from April, 2014

.Net Listing Classes or Methods With A Particular Attribute.

Whenever I build a website I always like to build an admin section and expose information such as which actions are cached, which controllers require authorization and which fields are required etc. Luckily in .Net MVC most of these things are implemented as attributes. Here is an example of scanning an assembly and listing all the classes and methods with a particular attribute on them. I chose two random attributes for this example: [Obsolete] public class Program { public static void Main(string[] args) { var program = new Program(); var types = program.GetAttributesOnClasses (); foreach (var t in types) { Console.WriteLine("Class: {0} - Attribute: {1}", t.Type.Name, t.Attributes); } var methods = program.GetAttributesOnMethods (); foreach (var method in methods) { Console.WriteLine("Method Name: {0} - Attribute: {1}", method.MethodInfo.Name, method.Attributes);...

Why don't more people use the .Net Settings as opposed to Config settings.

I see a lot of people putting key-value pairs in web.config/app.config files and then using them in their applications via var someConfigKey = ConfigurationManager.AppSettings["someConfigKey"]; int count; if (int.TryParse(someConfigKey, out count)) { //// Do Something } However I rarely see people using application settings. I won't go into depth on what application settings are as the Microsoft website has a pretty good explanation of them here however Settings are strongly typed and so the above lines of code can all be replaced with: var count = Properties.Settings.Default.Count; I might be missing something here but it seems like the latter is much more convenient to use if you are simply using the appSettings section of the .config file. Of course it is a different story if you are using .config sections which are serialized into objects but for the most part what I can figure is that many people don't know about .net Settings. Take this or this s...

VB.Net Gotchas

VB.NET is a very user friendly language however I find its user friendliness to be quite confusing and error prone, especially when switching back and forth between C# and VB. Simple things like VB's handling of null or concatenation can lead to bugs if your not careful. For example: Sub Main() Console.WriteLine("The value of Nothing is also the value of the default type: " & (Nothing = False)) Console.WriteLine("The value of Nothing is also the value of the default type: " & (Nothing = 0)) Console.WriteLine("The value of Nothing is also the value of the default type: " & (Nothing = String.Empty)) Console.WriteLine("The & operator with two integers concats both: " & (3 & 4)) Console.WriteLine("The + operator with two integers adds both: " & (3 + 4)) Console.WriteLine(...