c#
c#
After writing my previous post, Building your own Reflector with Mono.Cecil and the CodeDom, I decided to find other initiatives to writing a ‘new Reflector’. I’ve now joined the Monoflector effort, working on filling the gaps in the current decompiler. It seems a good fit, because I already knew a few things about it after my small spike. We’re working with the decompiler which is part of the Cecil project. However, there are quite a few bugs or just lacking features which makes reading the generated code quite frustrating. We’ve forked Cecil and are working on adding the features...
Sparked by the news that Red Gate’s Reflector will not be available for free anymore starting early March (see announcement on the redgate site), I decided to try how hard it would be to build your own reflector. Turns out, not too hard if you use the tools at hand. There’s actually already a nice effort going on based on the Mono.Cecil.Decompiler, but I only found out after I started my trials. What I basically did was get Mono.Cecil to read an assembly, hand me the MSIL statements and simply build an AST (Abstract Syntax Tree) out of that...
This code snippet made me chuckle: public static dynamic SomeMethod()
{
// ...
}
This is valid C# 4.0 code, it will compile. Just the notion that apparently something that’s static can be dynamic at the same time is funny (or so I think).
I’m always up for a challenge. I still have to watch the Mastering LINQ videos up on TekPub (which is amazing by the way, I encourage you to check it out!), but someone (hi Tuna!) tweeted about a small challenge related to the series. Basically, the challenge is to use a chained LINQ statement to create a prime number filter, without using any custom functions. Without further delay, here’s my solution: var primes = Enumerable.Range(1, 30)
.Where(x => x > 1)
.Where(x => x%2 != 0 && x != 2)
...
I just ran into a situation which could lead to very annoying latent bugs in your web application. Observe the following code. The view: <form method="post" action="Photo/Save">
<input type="text" name="Photo" />
<input type="text" name="Title" />
</form>
The Controller action:
public ActionResult Save(PhotoDTO photo)
{
// do something with the object
return RedirectToAction("Index");
}
The class:
public class PhotoDTO
{
public string Photo { get; set; }
public string Title { get; set; }
}
At first sight, there’s nothing wrong with this code. You’d say when the form is posted, the PhotoDTO object gets...
I had a short twitter conversation with Aaron Jensen on the decisions we made developing .Less. As I’ve blogged about before, for me personally it was more of a learning project than something with a set goal, at first. That aside, I had one thing in mind: being able to use less during development, without having to worry about it, add a handler and go! Less.Net, v0.1 My first solution was to simply install ruby and wrap the whole system call in a HttpHandler. Very clunky, but it worked! I blogged about this a while ago: Using LessCSS...
Recently I ran into a situation which isn't very obvious at first glance if you don't know how the Linq to objects extension methods work in C#. If you know how the IEnumerable<T> works in combination with the various extension methods in the System.Linq namespace however, you’ll quickly see what'll go wrong. Observe the following snippet: List<int> source = new List<int> { 1, 2, 3, 4, 5, 6 };
var filter = source.Where(x => x > 4);
foreach(var listitem in filter)
{
// do something with the value //
source.Remove(listitem);
}
This will throw an InvalidOperationException, stating that the collection has been changed. If you don’t...
One issue I’ve heard a few times already when explaining ASP.NET MVC to others is how to deal with common viewdata which should be available to every page. Sure, you can simply add it to the viewdata in each and every controller action you call, but that’s a horrible violation of the DRY (Don’t Repeat Yourself) principle. It’s also very error-prone, because you’re very likely to forget to add it some time or another. A more elegant solution to the problem is to have a base controller class which you will use for all your controllers. This is a...
In a standard ‘web 2.0’ (god, I hate that buzzword) web application, you’ll often have ajax calls to your code. I prefer using jQuery, but any method will do. A proper ajax request will set a header value in the HTTP GET request, and ASP.NET is a good boy and catches that. By checking if the IsAjaxRequest() method on the request returns true. This feature can be utilized for something I like to do on my ajax-exclusive controller actions: restrict access in such a way that only ajax requests can call the action. Other actions will get a 404,...