chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
I seem to have a problem using ObjectState against an object that derives from ContextBoundObject. As soon as it tries to create the state...
ObjectState sourceState = new ObjectState(myContextBoundObject);

...
it errors with...
failed: System.Runtime.Remoting.RemotingException : Remoting cannot find field 'myField' on type 'MyAssembly.MyContextBoundObject'.

PS. How do you search for exact matches on this forum?
PPS. Why do you send my password to me in clear text grrrr!?
asked by pkr (3k points)

7 Answers

0 votes
Hi
It's hard to tell just by one line ... :)
Can you post the test code?

How do you search for exact matches on this forum?

Try the Search link in the the forums page. You can fine tune your search over there.
In your case try using double quotes around the word like this: "ObjectState"

Why do you send my password to me in clear text grrrr!?

I'm not sure I understand. How should we send it?
answered by ohad (35.4k points)
0 votes
re: password
Don't send at ALL! I typed it in, I know what it is :D

When I get some time I can post the code but it's not that difficult, just need to mock an object that derives from ContextBoundObject...

public myClass: ContextBoundObject
{
private string myField;
}
answered by pkr (3k points)
0 votes
Hi,

I'm a bit confused about what you are trying to do here.

If you are trying to mock the object, and mean to set expectations on it, you can use this syntax (reflective mocks):

            Mock<DerivedClass> myDerivedObject = MockManager.Mock<DerivedClass>();
            myDerivedObject.ExpectAndReturn("MyString", "fakeValue");


Do note that you can set expectations on new methods OR overridden methods from MSCORLib. If this is not the case, you will not be able to set expectations on methods that are implemented in MSCORLib (ContextBoundObject is from there).

ObjectState is used for updating and retrieving values of non-public fields. So for instance, if your derived object looks like that:
    public class DerivedClass: ContextBoundObject
    {
        private int myField=5;
        ...
    }


You can set the field's value by using something like that:
            DerivedClass newDerived = new DerivedClass();
            ObjectState state = new ObjectState(newDerived);
            state.SetField("myField", 3);
            Assert.AreEqual(state.GetField("myField"), 3);


I ran a very basic test, containing a field in the derived object from ContextBoundObject, and it ran correctly. So assuming you do want to use ObjectState, can you post the whole code, so we can investigate?

Thanks.
answered by gilz (14.5k points)
0 votes
Hi,

We discussed this here, and it may depend on the type of the fields you are trying to access. A code snippet of those fields and how you access them through ObjectState may be helpful.

Thanks.
answered by gilz (14.5k points)
0 votes
Hi, sorry for the delay, I've managed to spend a few min's using a cut down project to replicate the (or a) problem. Turns out it's not quite as simple as I thought. Although removing the ContextBoundObject from my class does indeed allow ObjectState to work the reason isn't with ContextBoundObject but rather an AOP exception tracking class that only works if the class derives from ContextBoundObject. I can't include the code for the Exception Tracker (but can privatley) but it's pretty standard fare using IMessageSink to track the ins/outs of method calls.
NB. The original code for the AOP was from http://msdn.microsoft.com/en-us/magazine/cc164165.aspx
answered by pkr (3k points)
0 votes
Code I can include...

--

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplicationTypeMock
{
    [ExceptionTracking]
    public class ParentContextClass : ContextBoundObject
    {
        public ParentContextClass()
        {            
            this.myInnerClass = "hello there";
        }
        private string myInnerClass;
    }
}


using System;
using System.Collections.Generic;
using System.Text;
using TypeMock;

namespace ConsoleApplicationTypeMock
{
    class Program
    {
        static void Main(string[] args)
        {
            ParentContextClass parentContextClass = new ParentContextClass();
            MockManager.Init();
            {
                ObjectState sourceState = new ObjectState(parentContextClass);
            }
            MockManager.ClearAll();
            Console.WriteLine("end");
            Console.ReadKey();
        }
    }
}
answered by pkr (3k points)
0 votes
Hi,

I'll take you up on your offer. If you can send me a solution offline, that would be perfect. I'll send a separate email.

Thanks.
answered by gilz (14.5k points)
...