Discussion:
DynamicMethod and the use of an interface type
(too old to reply)
Hans Bertram
2008-08-04 08:21:01 UTC
Permalink
Hi,
When using a dynamic method in a generic list class.
It works fine as long as i fill the list with class objects, however if the
type is an interface i get an ArgumentException. {"Invalid type owner for
DynamicMethod."}
How can i make this DynamicMethod work for interfaces as well?

Example:
class MyClass :MyInterface
{
....

}

Type T is MyClass ...it works:
MyList<MyClass> list=new MyList<MyClass> ();
MyClass myObj=new MyClass();
list.Add(myObj);
...
if
Type T is IMyInterface.... i get the exception:
MyList<IMyInterface> list=new MyList<IMyInterface> ();
IMyInterface myObj=new MyClass();
list.Add(myObj);
...

//generic list ..

....
DynamicMethod dm = new DynamicMethod("MethodName", typeof(object), new
Type[] { typeof(T) }, typeof(T), true);
...
Jon Skeet [C# MVP]
2008-08-04 09:22:18 UTC
Permalink
Post by Hans Bertram
When using a dynamic method in a generic list class.
It works fine as long as i fill the list with class objects, however if the
type is an interface i get an ArgumentException. {"Invalid type owner for
DynamicMethod."}
How can i make this DynamicMethod work for interfaces as well?
class MyClass :MyInterface
{
....
}
MyList<MyClass> list=new MyList<MyClass> ();
MyClass myObj=new MyClass();
list.Add(myObj);
...
if
MyList<IMyInterface> list=new MyList<IMyInterface> ();
IMyInterface myObj=new MyClass();
list.Add(myObj);
...
//generic list ..
....
DynamicMethod dm = new DynamicMethod("MethodName", typeof(object), new
Type[] { typeof(T) }, typeof(T), true);
...
Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
--
Jon Skeet - <***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Hans Bertram
2008-08-04 15:01:01 UTC
Permalink
Hi Jon,
Thanks for the reply,
The problem is that passing a type of an interface as the owner type
generates the exception.
Passing instead the module it does not except.
This is still strange but it seems to work.

There are 8 overloaded constructors of the DynamicMethod class .

The one i used and gave a problem is:
public DynamicMethod (
string name,
Type returnType,
Type[] parameterTypes,
Type owner,
bool skipVisibility
)

The one which does not have a problem is:

public DynamicMethod (
string name,
Type returnType,
Type[] parameterTypes,
Module m,
bool skipVisibility
)



I fixed the problem by passing the module object instead of the owner.
Post by Jon Skeet [C# MVP]
Post by Hans Bertram
When using a dynamic method in a generic list class.
It works fine as long as i fill the list with class objects, however if the
type is an interface i get an ArgumentException. {"Invalid type owner for
DynamicMethod."}
How can i make this DynamicMethod work for interfaces as well?
class MyClass :MyInterface
{
....
}
MyList<MyClass> list=new MyList<MyClass> ();
MyClass myObj=new MyClass();
list.Add(myObj);
...
if
MyList<IMyInterface> list=new MyList<IMyInterface> ();
IMyInterface myObj=new MyClass();
list.Add(myObj);
...
//generic list ..
....
DynamicMethod dm = new DynamicMethod("MethodName", typeof(object), new
Type[] { typeof(T) }, typeof(T), true);
...
Could you post a short but complete program which demonstrates the
problem?
See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
--
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jon Skeet [C# MVP]
2008-08-04 15:30:05 UTC
Permalink
Post by Hans Bertram
Thanks for the reply,
The problem is that passing a type of an interface as the owner type
generates the exception.
Passing instead the module it does not except.
This is still strange but it seems to work.
There are 8 overloaded constructors of the DynamicMethod class .
public DynamicMethod (
string name,
Type returnType,
Type[] parameterTypes,
Type owner,
bool skipVisibility
)
public DynamicMethod (
string name,
Type returnType,
Type[] parameterTypes,
Module m,
bool skipVisibility
)
I fixed the problem by passing the module object instead of the owner.
Looking at the description of "owner" here, it wouldn't make much sense
to be an interface. I think it's meant to be a case of "pretend that
the code was actually defined in this type" - which doesn't work for an
interface, which can't have any code in it.

(The docs do say that it'll throw an exception if owner is an interface
type...)

Glad you've fixed the issue though.
--
Jon Skeet - <***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Barry Kelly
2008-08-04 18:52:43 UTC
Permalink
Post by Hans Bertram
Hi,
When using a dynamic method in a generic list class.
It works fine as long as i fill the list with class objects, however if the
type is an interface i get an ArgumentException. {"Invalid type owner for
DynamicMethod."}
How can i make this DynamicMethod work for interfaces as well?
The point of the owner is how the CLR will enforce visibility checks.
For an interface, there aren't any visibility constraints on a
per-member basis, so it doesn't really make sense to have an interface
as the method's owner.


-- Barry
--
http://barrkel.blogspot.com/
Loading...