Tag Archives: .net

Get the Moq Mock from a mock object

Update: See Vladimir’s comment below for the built in one-liner.

So there you in your code or possible your debugger and immediate window and you have a mock object and realise you want to change the setup but you didn’t keep a reference to the Mock<> …
So your first though is, no problem, you can cast your object to one of type Castle.Proxies.MyStronglyTypedProxyClass and voila you have access to the Mock property. Except that the compiler doesn’t recognise Castle.Proxies.MyStronglyTypedProxyClass as real class.
So then you try reflection to get hold of the Mock property. Which nearly works, but you get a System.Reflection.AmbiguousMatchException : Ambiguous match found. because there’s more than one property called Mock (one with and one without a generic parameter).
But this worked for me:

public static Mock<T> GetMockFromObject<T>(T mockedObject) where T : class
    PropertyInfo[] pis = mockedObject.GetType().GetProperties()
            p => p.PropertyType.Name == "Mock`1"
    return pis.First().GetGetMethod().Invoke(mockedObject, null) as Mock<T>;

hope that helps.

Asp.net / IIS7 : System.BadImageFormatException: Could not load file or assembly

You have something like this:

Server Error in ‘/Vendorapplication’ Application.
Could not load file or assembly ‘Oracle.DataAccess’ or one of its dependencies. An attempt was made to load a program with an incorrect format.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.BadImageFormatException: Could not load file or assembly ‘Oracle.DataAccess’ or one of its dependencies. An attempt was made to load a program with an incorrect format.
Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

It’s because you are trying to run 32 bit dlls on a 64 bit system. Fix it in IIS manager by changing the advanced settings on you application pool to enable 32 bit applications. Or create a new app pool specifically for them.

Alex James Brown has a nice picture: change app pool to 32 bit

There is a duplicate system.web.extensions/scripting/scriptResourceHandler section defined

You’ve been working with no problems probably in VS2008 on web app written on .net 2 or .net 3.5 or .net 3.5 sp1 and then you try running on a machine with windows 7 or vista running IIS7 and you get this error message…
Simples. It’s comes of running your .Net 2 (you did know that a .Net 3.5 app actually runs on .Net 2 didn’t you?) app under .Net 4. Fix it by changing the app pool for your web app to the pre-defined “Classic .Net App Pool”, which runs .Net framework 3
Run a dotNet 2 or dotNet 3 or dotNet 35 app under IIS7

Bob Martin’s eliminate boolean arguments tip – an example

I don’t always agree with Bob Martin’s Clean Code Tip #12: Eliminate Boolean Arguments – probably because the mathematician in me believes that there are such things as functions of a boolean value – but I came across an example in a WinForms app today where I’d apply it.

void FindApplicant(int id)
    applicant= getTheApplicantFromDataLayer(id);

turned out to mean:

void ProcessUI(bool processing)
       ... do a whole load of stuff

which would have been easier to read as:

void FindApplicant(int id)
    applicant= getTheApplicantFromDataLayer(id);

void DisableUIWhileUpdating()
void UpdateUIafterDataChange()
    ...do a whole load of stuff

C# : To return a comma delimited list from an array or collection of strings


            var description = new StringBuilder("");
            foreach (var item in productItems.Where(x => x.ItemType == type))
                description.Append(item.Description + ", ");
            return description.Remove(description.Length - 2, 2).ToString();


return string.Join(", ", productItems.Where(x => x.ItemType == type).Select(x => x.Description).ToArray());

because the first example fails when your collection has no elements.