Using Emit to Return a Value

  • Thread starter Thread starter Jehu Galeahsa
  • Start date Start date
J

Jehu Galeahsa

Hello:

I am trying to write a piece of code that can implement an interface's
methods simply by returning a specified value. I am trying to see how
Mock libraries are written.

For instance, I have a piece of code that looks like this:

MockFactory.CreateMock<ISomething>().Setup(something =>
something.Foo()).Returns("abc");

What I want to be able to do is somehow implement ISomething.Foo so
that it returns "abc". However, I am having a hard time figuring out
the Emit code for taking an arbitrary object and incorporating it.

I've never wanted to do something like this before, so I need some
pointers. Also, I am wondering if there is a fast way in MSIL to
request the default value of a type (null or 0) in case no return
value is specified.

Any help would be appreciated.

Thanks,
Travis Parks
 
Jehu said:
Does anyone know a way to quarantee a unique type name?

Unique relative to what? Assuming you put the type in your own specific
namespace where all the types are ones you've defined, you can guarantee
uniqueness however you like.

In other contexts, you may have to do some inspection of all the types
in a given namespace that have been loaded, to determine what would be
"unique".

In either case, a reasonably reliable way to create a type name that is
very likely to be unique (i.e. even though you should still check for an
already-existing type of the same name, it would be virtually impossible
for there to be a collision) would be to use the System.Guid type and
create a GUID to use in (or as) the type name.

Pete
 
Does anyone know a way to quarantee a unique type name?

If you mean a unique name for classes that you generate yourself
at runtime, then one obvious approach is to use C1, C2, C3 ... !

Arne
 
Unique relative to what? Assuming you put the type in your own specific
namespace where all the types are ones you've defined, you can guarantee
uniqueness however you like.

In other contexts, you may have to do some inspection of all the types
in a given namespace that have been loaded, to determine what would be
"unique".

In either case, a reasonably reliable way to create a type name that is
very likely to be unique (i.e. even though you should still check for an
already-existing type of the same name, it would be virtually impossible
for there to be a collision) would be to use the System.Guid type and
create a GUID to use in (or as) the type name.

Note that only N formatting gives a valid type name.

Arne
 
Hello:

I am trying to write a piece of code that can implement an interface's
methods simply by returning a specified value. I am trying to see how
Mock libraries are written.

For instance, I have a piece of code that looks like this:

MockFactory.CreateMock<ISomething>().Setup(something =>
something.Foo()).Returns("abc");

What I want to be able to do is somehow implement ISomething.Foo so
that it returns "abc". However, I am having a hard time figuring out
the Emit code for taking an arbitrary object and incorporating it.

I've never wanted to do something like this before, so I need some
pointers. Also, I am wondering if there is a fast way in MSIL to
request the default value of a type (null or 0) in case no return
value is specified.

Any help would be appreciated.

Thanks,
Travis Parks

After spending about two days searching for a way to stack an
arbitrary value using Emit, I decided that I would instead generate a
backing field for each return value. When I instantiate an instance of
my mock object, I simply loop through a list of methods and properties
and creating backing fields. After I create my type and instantiate an
instance, I loop through the return values and assign them to the
backing fields. It is kind of nasty, but it works. I suppose the next
challenge is finding performance boosts, throwing exceptions and
allowing hit counts to be evaluated. More importantly, there has to be
a way of making all of this emit code look a little less intimidating.
 
Back
Top