Quantcast
Channel: ClearScript
Viewing all articles
Browse latest Browse all 2297

New Post: access private fields of base class

$
0
0
Greetings jnair, and thanks for your question!

Unfortunately ClearScript doesn't have a complete solution for private member access. As you've discovered, PrivateAccess only goes so far, and casting doesn't propagate it.

Here's one thing you can do. Let's say your classes look like this:
publicclass FooBase {
    privateint PrivateBaseField;
}
publicclass Foo : FooBase {
    privatestring PrivateField;
}
You can expose the base portion of your object separately, with its own PrivateAccess flag:
var foo = new Foo();
engine.AddHostObject("foo", HostItemFlags.PrivateAccess, foo);
engine.AddRestrictedHostObject("fooBase", HostItemFlags.PrivateAccess, (FooBase)foo);
engine.Execute(@"
    foo.PrivateField = 'bar';
    fooBase.PrivateBaseField = 123;
");
It's a bit ugly, but it works :)

Cheers!

Viewing all articles
Browse latest Browse all 2297

Latest Images

Trending Articles



Latest Images