This project is read-only.

Finding a Type

Mar 29, 2011 at 1:31 AM

I'm looking at the documentation and some samples and I'm not entirely sure how to proceed. What I want to do is to be able to find a type in order to make a reference and I'm not sure if I need to create my own platform for this or not. For example I am setting up my host like this:

var nameTable = new NameTable();
using (var host = new PeReader.DefaultHost(nameTable)){
  var coreAssembly = host.LoadAssembly(host.CoreAssemblySymbolicIdentity);
  // ...

Some where in that ... what I would like to do is take the host and get a Type reference, something "types = like host.FindTypes(imports, typeName)" which would let met find all Types that match any combination of typeName and import from the imports specified.  I see that the PlatformType class is almost what I want to use and has some helpers for creating NamespaceTypeReference objects. My question is, do I need to derive from that in order to make the methods I want?


Mar 29, 2011 at 2:17 AM

One other thing, PlatformType has a public CreateReference(...) but IPlatformType does not. What I am wanting to do is to create my own ITypeResolver which has a FindType method but the intance just calls into host.PlatformType.CreateReference() once I have enough information. Is there a reason why those methods aren't expose on IPlatformType?

Mar 29, 2011 at 2:54 AM

The interfaces represent an immutable object model and it was felt that CreateReference represents a mutator since it can add entries to the host's name table. I might be persuaded that this is not such a big deal, but I need to think through the issue a bit more. The main question is: if an IPlatformType instance is marshalled by reference from a privileged AppDomain to an AppDomain with low priveleges, can the latter AppDomain "attack" the privileged domain by maliciously filling up its name table via CreateReference call?

Mar 29, 2011 at 4:38 AM

Interesting, I wasn't expecting you to say something like that.

How about this, on IMetadataHost there is a LoadedUnits member that let's you search through the types already. What if on ITypeDefinition (or whereever is most appropriate) there was a ToReference() member so that it's possible to create references to types you have access to but not to be able to arbitrarily create any number of name table entries?

Otherwise I should create a Host that inherits from PeReader.DefaultHost and implement a custom platform that inherits from PlatformType and also my custom ITypeResolver? Does that sound right?

Mar 29, 2011 at 10:57 PM

Ok nevermind actually, I'm blind. I can see that the types that implement ITypeDefinition do actually also implement ITypeReference. It's a little tricky sometimes when you get an object back of one interface type but then you have to know to cast it to another! Please ignore this whole question.