In previous post I showed how to build a simple expression evaluator using expression trees. Although it works fine it has some drawbacks:
To solve these issues we will use a different algorithm and deal with all points except variable support.
This is first part of a series about writing expression evaluator in C#. Even though there are several of them available already I decided to write my own as this one is based on expression trees and is very simple. The evaluator supports numeric expressions as well as expression with parameters.
This is table of contents for Building Expression Evaluator with Expression Trees in C# series. We are going to build a simple mathematical expression evaluator in C# using expression trees. The library supports simple expressions such as 2.5+5.9, 17.89-2.47+7.16, 5/2/2+1.5*3+4.58, expressions with parentheses (((9-6/2)*2-4)/2-6-1)/(2+24/(2+4)) and expressions with variables: var a = 6; var b = 4.32m; var c = 24.15m; Assert.That(engine.Evaluate("(((9-a/2)*2-b)/2-a-1)/(2+c/(2+4))", a, b, c), Is.EqualTo((((9 - a / 2) * 2 - b) / 2 - a - 1) / (2 + c / (2 + 4)))); At the end of the series full source code will be available at github and the library will be published to NuGet.
In previous post I showed an approach combining DynamicObject with ViewState for accessing ViewState members using dynamic properties instead of string indexes. However that approach had some disadvantages:
In order to overcome these issues I made several changes to the DynamicViewState class.
If you have worked with ASP.Net MVC 3 you are probably aware of ViewBag property which allows you to pass data from Controller to View by using dynamic properties instead of hard coding strings. While the properties still are not strongly typed and compiler does not check them it can provide an advantage. For example you can avoid casting as it returns dynamic objects. This post will show how to use the same approach in ASP.Net Web Forms for ViewState property giving us a dynamic viewstate class.
AppDomain.AssemblyResolve event can be used to load referenced assemblies at runtime. This can be useful in multiple scenarios: you can use it to load a library from a different location or load a library based on bitness or load a library which is embedded in the executable file. In all cases there are some tips and tricks which can help you to easily implement the event handler and avoid exceptions.
Source code for this article is available at Silverlight Remote Control Source Code
Native Extensions For Microsoft Silverlight is a collection of COM automation based runtime libraries and Silverlight libraries which allow elevated trusted out of browser applications to utilize windows platform feature. For example you can build applications which use Sensor API, Speech API, react to remote control, integrate with windows 7 taskbar or listen to windows messages sent to the host window. In this post I will build a simple slideshow application to demonstrate some of the capabilities of the library.
As you know retrieving process path in .Net is quite straightforward. For example if you have process instance
someProcess you can get the process path by accessing
someProcess.MainModule.FileName However for elevated processes the previous code snippet throws Win32Exception.
While working on an application which makes use of Process class I found several bugs which occur in special conditions. More specifically accessing
HasExited properties causes a
NativeErrorCode equal to five. This indicates that the exception is caused by not having enough rights to retrieve the required information.
The exception occurs under the following conditions: