Dynamically Call A Method, Using a String with Method Name

I was trying to set up a dynamic setup of algorithms in server side code which return true if they succeed, but I wanted users to be able to choose from a database generated list as to which algorithm they wished to run, unfortunately I wanted to be able to update the list dynamically without having to rebuild all the code, only the algorithm static class that contains the algorithm and the database that contains the names of all the classes but not modify the presentation layer.

After having a quick look around I found this code on an old faithful website codeproject.com with the perfect article which contained a simple solution to my problem.

Dynamically Invoke A Method, Given Strings with Method Name and Class Name by Matt Perdeck.

The solution I had only required some minor modification to make it work for me,

and to call it

Building code to pass an arbitrary string containing the name of an object property and replacing it with the value of that property using RegEx, Reflection, and Linq

I recently wanted for the outputs of a search to be configurable easily by an admin by storing a specially crafted string which would use the object returned by the query and only display certain parameters, the key was that different people could see different things. The solution was a specially crafted string containing the entire formatting of the output with sections of interest marked out by brackets.

My first problem was getting the names of all the properties within the object, as I didn’t want to have to change my code if the object changed, or if I one day decided to change the name of one of the object’s properties. I did this after playing around within System.Reflection and after searching for a while I eventually stumbled upon an answer within StackOverflow by one of the users @mythz.

Then simply using a little System.Linq code to filter the responses and RegEx to get the sections to be replaced.

JQuery UI Modal Dialog triggering ASP.Net Updatepanel Control Problem

I had this problem recently where I had a series of links which triggered a JQuery UI Modal dialog which when clicked was supposed to trigger an async-postback method which deletes an entry. The problem was the Modal Dialog.
First is the section with the modal dialog message and the button to trigger the postback.

It seemed that when I used the dialog, the click event occurred, but the async-postback did not occur.

However if I removed the dialog and just wrote the code to run one after another like below it worked as expected and triggered the button, though as you would expect minus the confirmation dialog.

As you could imagine, this was beginning to cause me headaches when I finally discovered after posting details about the problem on StackOverflow. The Modal Dialog only worked with a slight change of code. I needed to wrap the click event in a setTimeout() function. I’m not sure why this works, but it does.

Many thanks to @DanaBenson for his answer.

Bah Symbols, dealing with non-unicode symbols in HTML pages

I recently had an issue with a scientist posting an abstract which contained the alpha and beta characters, as well as a few others and I was facing the issue that they were not appearing in the browser. Initially I thought this was because the browser was not rendering Unicode as it’s output format, and I went through several iterations of changing headers and adding meta-tags to output as UTF-8, which I really should have been doing anyway.

This however did not solve my problem, the problem was that the characters I was trying to display were not Unicode, they were symbols with a Unicode identifier of U+F061 and U+F062. What I tried at first was to encode the HTML characters to their HTML unicode entity, using the code found in a CodeProject article.

This while making it more accessible to the browser, also did not solve the problem as despite them being valid elements, the browser did not understand them. I discovered in the end the problem was down to the font. The Symbol font to be exact. So with a little modification to the method to include the font-family style attribute.

References

Removing empty entries from a string array

I recently answered this question on stack overflow: Find and remove items from array in c# which is in the format below.

In this case the items to be removed are whitespaces. So the simple solution would be to use Linq which can easily filter out entries where they are empty and return the filtered array.

Another possible solution is to skip the problem entirely. This would depend on how the array is filled in the first place, but if you were splitting a string which is how I would typically end up with this kind of array, you can do the below. This would mean that empty entries would never get into the array.

Ordering and Removing Duplicates from a C# IList using System.Linq

I have recently been delving into dealing with ILists Containing Objects, and have a need to order them without doing another database query with an Order By statement. Previously I would have dealt with this situation by looping through the elements one at a time and if they matched a set of rules put them in and if not ignore, in other words a lot of effort for the same gain. What I should have been doing was using the wonderfulness of System.Linq to solve my duplication filtering and sorting woes, as below.

Re-triggering JavaScript methods after partial page updates without using ASP.Net Ajax Control Toolkit “UpdatePanelAnimationExtender”

ASP.Net Ajax Control Toolkit has a cool gadget called the UpdatePanelAnimationExtender control which sits at the bottom of the page and allows for specific events to occur, while an update panel updates and when it is finished. For the example I had a JavaScript function called loadonhoverEvent(), see below which caused every <td> element in a tablerow’s background to darken when the user hover over the element. Seems simple enough.

However there was a minor problem, one of the controls only did a partial page postback to toggle the status of one of the fields. No Problem, Use  UpdatePanelAnimationExtender, however this is a bit overkill for what I was intending.

All I needed was

The only problem I was having was I initially has the code as a self-executing function, styled after the jQuery source code which does the same. That would mean I would not need the $(document).ready() section as below:

But this would not work as it cannot call the function anymore, so I had to break it out of the self-executing block.

MSQL Getting the Integer Value from a Hex Value that looks like a Decimal

Background

Just to give you come context I have included a little background as to why I’m trying to do this:

I have a table ‘cp’ which has an [id] field which is the identifier for each registration I have come realize that this is to be moved from a deployed solution to a cloud based as a result multiple accounts will be accessing the same objects and it would be best if they all start at 1 so rather than use the id field I have created an int field called label which will be used in conjunction with the account id to find the unique record per account.

When the registrations are printed out on the screen I want them to be displayed as a 5 digit HEX number and I can simply do the following in the c# code

value.ToString(“X5”)

However the problem existed with the existing entries in the database before I added the new label field. I have found several questions on StackOverflow with a similar topic but did not answer my question

The Problem

i have the fields with ids (1,2,3,—-,11,12,13,—etc) values 1-9 can simply be copied over as the value.ToString(“X”) will simply do nothing with those values however if I simply copied value 11 would return “B” which correctly is the HEX value of 11 however I want it to return “11” so I need an SQL script which will convert 11 => 17 (which is the hex value of 0x11) so that when the application reads it it will output the value 11 to the screen.

This is only to occur once to convert the existing ids, which is why I’m wanting an SQL script to do it all in one batch rather than build it into the application.

What I would Like would be something like below so that it assumes the value in would be a hex value and convert it back to an Int.

UPDATE [cp] SET label_id = CONVERT(INT, ‘0x’ + id);

I found that I could get the correct effect in C# by doing the following

int.Parse(value.ToString(),System.Globalization.NumberStyles.HexNumber).ToString()

The Solution

I asked this question on StackOverflow But as it turned out I managed to find a solution before anyone answered the question.

It was not the prettiest answer but could be used for multiple use-cases and I liked the simplicity of it.

Then it can simply be called using

UPDATE [cp] SET label = dbo.ConvertFromBase(id,16);

I would like to thank the original author of this code Converting Hexadecimal or Binary to Decimal on SQL Server 2008 and 2005, though I am not using it exactly at the writer intended it is brilliantly simple.