c# - Get assembly reference without reflection or a known type -
in current framework's design, peruse types of specific assemblies , work based on types found. assemblies searches through determined application bootstrapper/initializer. assemblies compiled in, can referenced via: typeof(sometypeintheassembly).assembly
. nice because bootstrapper code has strong reference type in assembly , there's no fumbling qualified names inline strings need manually kept up-to-date if assembly qualified name changes. didn't referencing unrelated type in assembly , being dependant on type being there. (what if moves assembly? if deprecate/delete type? change namespace?) in code, looks bit weird too:
frameworkassemblyreader.read(typeof(someassemblynamespace.subnamespace.graphingcalculator).assembly);
now in bootstrapping code, have direct dependency (although pretty trivial dependency) on graphingcalculator
has nothing bootstrapping stage (and graphingcalculator, has nothing obtaining assembly references). avoid this, in each assembly intend use in way, added class in root:
namespace someassemblynamespace { public static class assemblyreference { public static system.reflection.assembly { { return typeof(assemblyreference).assembly; } } } }
which isn't bad because bootstrapping code looks like:
frameworkassemblyreader.read(someassembly.assemblyreference.get);
but now, have dozen or assemblies assemblyreference
class copy/pasted , expect continue grow applications built ontop of framework. question is, there nice way avoid assemblyreference
class duplication , still programmatically pass in assembly references base framework, yet avoid fuzziness of pointing arbitrary/unrelated type in target assembly? googlings seem tell me there's no api or language feature lets me point assembly (like typeof
classes), i'm hoping here has come better solution have avoids class/code duplication. of note, code compiled against silverlight know can tend limit of api/techniques. thanks!
edit: add monkey wrench, based on answer "the coon", should mention don't load same assemblies. example, might have "carbuilding" assembly that's applicable applications not all. it's bootstrappers tell me ones wish use (along custom ones developer employing)
editx2: answer jon skeet's question: clients (or internally) create projects/dlls wish support application. in turn, these projects reference internal base framework. projects may contain resources (3d files, images, text, localization, etc.) , plugin-esque classes (they implement scripts discover , instantiate/execute needed during lifetime of application). additionally, our system supplies optional modules (dlls) contain reusable plugins/content clients (or us) can leverage when making specific applications. might see project structure like:
solution ->myappbootstrapper_android ->myappgui_android ->myappframework ->myappresources ->myappadditionalresources_android ->myappbootstrapper_silverlight ->myappgui_silverlight ->myappframework ->myappresources ->myappadditionalresources_silverlight ->baseframeworkgraphingmodule
the bootstrapper projects wire dependencies , provide projects/dlls (assemblies) should provided base framework discovery. note in case, "myapp" shares own mini-framework , shared resources across both android , silverlight builds, both have own references independent 1 another. silverlight 1 takes advantage of baseframeworkgraphingmodule
, have own resources specific platform.
so in bootstrappers in projects (albeit simplified):
namespace myappbootstrapper_android { public class bootstrapper { public void setup() { frameworkassemblyreader.read(myappgui_android.assemblyreference.get); frameworkassemblyreader.read(myappframework.assemblyreference.get); frameworkassemblyreader.read(myappresources.assemblyreference.get); frameworkassemblyreader.read(myappadditionalresources_android.assemblyreference.get); } } } namespace myappbootstrapper_silverlight { public class bootstrapper { public void setup() { frameworkassemblyreader.read(myappgui_silverlight.assemblyreference.get); frameworkassemblyreader.read(myappframework.assemblyreference.get); frameworkassemblyreader.read(myappresources.assemblyreference.get); frameworkassemblyreader.read(myappadditionalresources_android.assemblyreference.get); frameworkassemblyreader.read(baseframeworkgraphingmodule.assemblyreference.get); } } }
so each application built on top of base framework, bootstrapper created indicates assemblies include (and other irrelevant wiring work). answer question specifically, project has compile-time references each assembly needed (this pulls double duty ensures dlls packaged android/silverlight deployment) not dynamically downloaded user's smartphone or silverlight app packaged in initial download. question how bootstrappers know assemblies use, developer knows , places necessary frameworkassemblyreader.read
calls. hope helps! taking time @ it. have feeling i'm making out of nothing (or missing far better solution/design altogether).
finally, neglected (stupidly) mention compile against mono android, , in near future, wpf. winrt addition in future i'll happy cross bridge when come it. though, since each compiled in own way own platform features, don't mind terribly if different platforms have different way of pulling in assembly references; though nice if there unified syntax between platforms.
editx3: yeah, one. mentioned, didn't clarify example not projects need or should read base framework. example, utility or business logic projects have code isn't relevant base framework relevant client application. above example:
solution ->myappbootstrapper_android ->myappgui_android ->myappframework ->myappresources ->myappadditionalresources_android ->mycompanysharedutilities ->myappbootstrapper_silverlight ->myappgui_silverlight ->myappframework ->myappresources ->myappadditionalresources_silverlight ->baseframeworkgraphingmodule ->mycompanysharedutilities
mycompanysharedutilities
leveraged myappgui_silverlight, , myappframework developer might not run through frameworkassemblyreader.read
because contains, say, proprietary implementations of math, or business rules.
i'm not sure how expect clients use code, suggestion may irrelevant, wouldn't make more sense away setup function (at least, hide client) , use kind of configuration file?
<referencedassemblies> <referencedassembly key="unchangingkey" qualifiedname="someassemblyname, version=1.0.0.0, culture=neutral, publickeytoken=0123456789abcdef" /> </referencedassemblies>
load assemblies config file. using key, don't have update application if qualified name changes, update config file (and leave key as-is).
this is, basically, approach visual studio uses project files. here's example 1 of vb projects:
<itemgroup> <reference include="system.core"> <requiredtargetframework>3.5</requiredtargetframework> </reference> <reference include="system.drawing" /> <reference include="system.windows.forms" /> <reference include="system.xml.linq"> <requiredtargetframework>3.5</requiredtargetframework> </reference> <reference include="system.data.datasetextensions"> <requiredtargetframework>3.5</requiredtargetframework> </reference> <reference include="uiautomationprovider"> <requiredtargetframework>3.0</requiredtargetframework> </reference> <reference include="windowsbase"> <requiredtargetframework>3.0</requiredtargetframework> </reference> <reference include="presentationcore"> <requiredtargetframework>3.0</requiredtargetframework> </reference> <reference include="presentationframework"> <requiredtargetframework>3.0</requiredtargetframework> </reference> <reference include="system" /> <reference include="system.data" /> <reference include="system.xml" /> </itemgroup>
edit:
here's thought... if used reflection.emit generate assembly xml config file? add stub class generated assembly core project use create hard reference generated assembly, , use reflection probe each reference assembly (in xml file) create hard reference object in referenced assemblies. you'd need regenerate assembly when 1 of referenced assemblies changes, require no code changes. build assembly generation code core project users able update projects needed.
Comments
Post a Comment