[mono-android] Creating a type from another assembly by name
Konaju Games (Dev)
dev at konaju.com
Sun Feb 12 23:18:11 UTC 2012
(resending from the correct email address this time)
If you know XNA on Windows, Windows Phone or Xbox 360, this will make more
sense. If not, I'll give it a shot at explaining how it works.
MonoGame is a free, open implementation of Microsoft's XNA Framework for
the various platforms that Mono supports. MonoGame.Framework provides the
functionality to read assets from XNB files. These XNB files contain the
fully qualified type names of the ContentTypeReader-derived class that
should be used to read various sections of the XNB file (ModelReader,
Texture2DReader, etc). These XNB files are generally created by building a
XNA project on Windows or Windows Phone.
The game can provide custom ContentTypeReader-derived classes for
game-specific asset formats. The fully qualified type name of these custom
readers are also stored in the XNB so the Framework knows which object to
create to read the asset. These custom readers are usually kept in their
own assembly to make the asset building easier on Windows Phone. So my
(simplified) .sln has three projects: Game, Game.Data and
MonoGame.Framework. Game references Game.Data and MonoGame.Framework.
Game.Data references MonoGame.Framework. MonoGame.Framework needs to
create an object from a class that resides in Game.Data but it cannot have
a reference added Game.Data because MonoGame.Framework is a separate entity
used across many different projects.
In this case, Game.Data contains a class Game.Model.CpuSkinnedModelReader.
In MonoGame.Framework, the fully-qualified type name read from the XNB
file is processed a bit and passed to Type.GetType(). This should return
the Type for the given name, but in the case of readers in the Game.Data
assembly, it is returning null.
I have made sure the reader type is not getting stripped (because nothing
explicitly references it) by using the [Preserve] attribute and also the
falseflag approach.
The code in MonoGame.Framework to create a ContentTypeReader looks similar
to this:
ContentTypeReader CreateTypeReader(string readerTypeName)
{
ContentTypeReader reader = null;
Type readerType = Type.GetType(readerTypeName);
if (readerType != null)
{
reader = (ContentTypeReader)Activator.CreateInstance(readerType);
}
return reader;
}
If I call CreateTypeReader("Game.Model.CpuSkinnedModelReader, Game.Data")
it returns null because Type.GetType() returned null.
Is there any trick or extra bit I should be doing in order to instantiate a
class from another assembly?
Steve 'Sly' Williams
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ximian.com/pipermail/monodroid/attachments/20120213/486093cc/attachment.html>
More information about the Monodroid
mailing list