Category Archives: Code

Software Development

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.

Notes on the slide, Marriage According the Bible

A friend forwarded me a slide entitled “Marriage According to the Bible” which suggests that marriage according to the bible is mostly about men claiming sexual rights and women (especially rape victims, prisoners of war, polygamously-married women and widows) having to lie back and take it. The slide raises questions that are worth serious debate, and especially I can see that faced with someone saying ‘bring back the Old Testament law in the modern world!’ one would be somewhat alarmed. But I think my outline ‘opening argument for the defense’ would be something like the following. I say ‘defense’ because in spite of it’s neutral tone, the slide does to me meant as an attack. It’s not an objective reading certainly. But more on that below.

Rape, polygamy, prisoners of war and slavery were never fair in the ancient world any more than in the modern world. They happened and still happen. Your choices as Government of the realm are, regulate for it or pretend it’s not happening, but modern governments (for all their powers undreamed-of by predecessors 3 millenia ago) are still unable to erase them.

I think the law of Moses pragmatically addresses problems that we no longer have in the western world. For most of us (not being either iron age subsistence farmers or ancient historians) we don’t see the problem being addressed. We certainly don’t ‘get’ the offered solution.

The most serious problem for a subsistence farming community (I believe that was 99% of the world until a few hundred years ago) was, ‘how will I keep me and my family alive for the next 6 months?’ To get a feel for what that’s like, you could try imagining being unemployed for a year longer than all your money, savings, and sale of possessions would last. Then go back 100 years (before the welfare state) and consider your options. Begging? Stealing? Prostitution? Then move to a country where average annual annual income is less than 2 dollars a day (In 2011, that’s nearly half the world according to Preferable of course you could fall back on family, if they’re alive. But if they aren’t …

So the problem to address is: how will a rape victim, prisoner of war or widow keep herself and children alive if she’s one of the 99% of the world who aren’t wealthy.

I do think that the author of the slide has just misread the obligations of the OT law. The marriage obligations were obligations on the man not the woman. The point is to protect a woman from destitution, by not leaving her without an income.
– a rape victim was not obliged to marry her rapist, that’s an error on the slide. Rape was a capital offence, she could push for the death sentence.
– the reference cited on the slide under rape is rather the case of a man who ‘seduces’ (I did check the word in Hebrew, I don’t think there’s any better or more certain translation) an unattached woman: he (not she) becomes obliged to marry her rather than abandon her, a point that a reader of Jane Austen would feel the force of.
– ‘levirate’ marriage (you husband dies, you marry his brother) is again an obligation on the man not the woman. I think it’s fairly clear in the text that the point of the law is that the woman can legally claim the right to a replacement husband (Deut 25:5ff). Husbands keep the wolves from the door. And other predators.
– I suppooooose it’s true that a wife ‘must submit sexually’ although I think that bit of the slide is a little bit made up; there’s no citation for it. Again the obligation is more enforceable the other way round: I think a woman whose husband failed to provide food, clothing or ‘marital rights’ had, if all else failed the option of appeal to legal rights to those 3 things. A new testament perspective on ‘sexual rights’ is “a husband’s body does not belong to him but to his wife, and vice versa.” You can see it’s a joint enterprise view, not an individual rights view, of marriage.

So, I’d say that to produce this slide you’d have to (1) not really know how life even as little as 500 years ago differed economically and therefore socially from our own and (2) read the text with some hostility. I don’t see you could produce this slide from a fair minded attempt to ask “what did this law mean for a woman 3000 years ago”

Am I being unfair? There is a serious debate to be had about some of it, I guess the above is the opening argument, not necessarily a final position.

I want to set a Windows Environment Variable for my whole user login session

Use setx to permanently set an environment variable

If you’ve used windows environment variables at all, you’ve probably wished you could set them in a command window for use in another program.
You can’t change the environment of an already-running program – the environment is copied when a program starts – but you can set environments values for your user session that will be picked up by any new programs you launch with setx:

setx myvariable thenewvalue

Much easier than right-clicking to get to My Computer properties.
setx /? will also tell you about setting permanent, machine-wide and remote machine environment values.

setx comes with Windows 7 or in the Windows Resouce Kit.

How to unset a variable set with setx

setx myvariable ""

See for more details. / 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