Files
buildbroken-blog-archive/archive/wayback-html/reflection.html
T
2026-08-16 19:03:44 +02:00

837 lines
76 KiB
HTML
Vendored

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- saved from url=(0106)https://web.archive.org/web/20090629065657/http://blog.aztec-project.org/2009/06/24/wozu-dient-reflection/ -->
<html xmlns="http://www.w3.org/1999/xhtml" class=" kivqwpjg idc0_350"><head profile="http://gmpg.org/xfn/11"><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><script src="./reflection_files/athena.js.Download" type="text/javascript"></script>
<script type="text/javascript">window.addEventListener('DOMContentLoaded',function(){var v=archive_analytics.values;v.service='wb';v.server_name='wwwb-app223.us.archive.org';v.server_ms=356;archive_analytics.send_pageview({});});</script>
<script type="text/javascript" src="./reflection_files/bundle-playback.js.Download" charset="utf-8"></script>
<script type="text/javascript" src="./reflection_files/wombat.js.Download" charset="utf-8"></script>
<script>window.RufflePlayer=window.RufflePlayer||{};window.RufflePlayer.config={"autoplay":"on","unmuteOverlay":"hidden"};</script>
<script type="text/javascript" src="./reflection_files/ruffle.js.Download"></script>
<script type="text/javascript">
__wm.init("https://web.archive.org/web");
__wm.wombat("http://blog.aztec-project.org:80/2009/06/24/wozu-dient-reflection/","20090629065657","https://web.archive.org/","web","/_static/",
"1246258617");
</script>
<link rel="stylesheet" type="text/css" href="./reflection_files/banner-styles.css">
<link rel="stylesheet" type="text/css" href="./reflection_files/iconochive.css">
<!-- End Wayback Rewrite JS Include -->
<title>build broken » Wozu dient Reflection</title>
<meta name="generator" content="WordPress 2.8">
<meta name="theme" content="Mandigo 1.41">
<link rel="stylesheet" href="./reflection_files/style.css" type="text/css" media="screen">
<style type="text/css">
body {
font-family: Arial, Sans-Serif;
background-color: #44484F;
}
a {
font-weight: bold;
}
.narrowcolumn .entry, .widecolumn .entry {
line-height: 1.4em;
}
.inline-widgets #wp-calendar caption, .blogname, .blogdesc {
font-family: 'Trebuchet MS', 'Lucida Grande', Verdana, Arial, Sans-Serif;
}
.blogname {
font-size: 4em;
margin-top: 15px;
}
.posttitle, #comments, #respond .title, #respond.pre27, #trackbacks {
font-family: 'Trebuchet MS', 'Lucida Grande', Verdana, Arial, Sans-Serif;
}
.commentlist li, #commentform input, #commentform textarea {
font-family: 'Lucida Grande', Verdana, Arial, Sans-Serif;
}
#commentform p {
font-family: 'Lucida Grande', Verdana, Arial, Sans-Serif;
}
.sidebars {
font-family: 'Lucida Grande', Verdana, Arial, Sans-Serif;
}
#page {
width: 987px;
}
#header {
background: url(https://web.archive.org/web/20090629065657im_/http://blog.aztec-project.org/wp-content/themes/mandigo/images/header-1024.png);
height: 243px;
width: 987px;
}
#headerimg {
height: 226px;
width: 961px;
background: url(https://web.archive.org/web/20090629065657im_/http://blog.aztec-project.org/wp-content/themes/mandigo/schemes/orange/images/head-1024.jpg) bottom center no-repeat;
}
#main {
background: url(https://web.archive.org/web/20090629065657im_/http://blog.aztec-project.org/wp-content/themes/mandigo/images/bg-1024.png);
width: 957px;
}
#footer {
background: url(https://web.archive.org/web/20090629065657im_/http://blog.aztec-project.org/wp-content/themes/mandigo/images/foot-1024.png);
}
.post {
text-align: justify;
background: #FAFAFA;
border: 1px solid #EEEEEE;
}
#footer {
width: 987px;
}
.sidebars {
background: #EEEEEE;
border: 1px solid #DDDDDD;
}
#sidebar1 .sidebars {
width: 210px;
}
#sidebar2 .sidebars {
width: 210px;
}
.entry img {
float: none;
}
.sidebars li {
list-style-image: url(https://web.archive.org/web/20090629065657im_/http://blog.aztec-project.org/wp-content/themes/mandigo/schemes/orange/images/star.gif);
}
.commentlist {
}
.commentlist li {
list-style: none;
}
#wp-calendar {
margin: 0 !important; margin-top: -1.5em;
}
#wp-calendar caption {
margin-top: -1.4em;
}
/* dirty fix for the event calendar plugin */
div#wp-calendar .nav {
margin: 0;
}
/* end dirty fix */
.datestamp div {
font-family: 'Lucida Grande', 'Lucida Sans Unicode', Arial, Sans-Serif;
}
.cal3 {
}
.pages {
text-align: right;
}
.pages ul li.has_sub {
background: url(https://web.archive.org/web/20090629065657im_/http://blog.aztec-project.org/wp-content/themes/mandigo/images/icons/bullet_arrow_right.png) no-repeat center right;
}
.head_overlay {
background: url(https://web.archive.org/web/20090629065657im_/http://blog.aztec-project.org/wp-content/themes/mandigo/images/head_overlay.png);
}
</style>
<link rel="stylesheet" href="./reflection_files/scheme.css" type="text/css" media="screen">
<link rel="alternate" type="application/rss+xml" title="build broken RSS Feed" href="https://web.archive.org/web/20090629065657/http://blog.aztec-project.org/feed/">
<link rel="pingback" href="https://blog.aztec-project.org/xmlrpc.php">
<link rel="alternate" type="application/rss+xml" title="build broken » Kommentare-Feed zu Wozu dient Reflection" href="https://web.archive.org/web/20090629065657/http://blog.aztec-project.org/2009/06/24/wozu-dient-reflection/feed/">
<script type="text/javascript" src="./reflection_files/comment-reply.js.Download"></script>
<link rel="EditURI" type="application/rsd+xml" title="RSD" href="https://blog.aztec-project.org/xmlrpc.php?rsd">
<link rel="wlwmanifest" type="application/wlwmanifest+xml" href="https://blog.aztec-project.org/wp-includes/wlwmanifest.xml">
<link rel="index" title="build broken" href="https://blog.aztec-project.org/">
<link rel="start" title="Umsetzung des Plug-In-Patterns" href="https://blog.aztec-project.org/2009/06/05/umsetzung-des-plug-in-patterns/">
<link rel="prev" title=".NET-Webservice mit ASP-Classic ansprechen" href="https://web.archive.org/web/20090629065657/http://blog.aztec-project.org/2009/06/19/net-webservice-mit-asp-classic-ansprechen/">
<meta name="generator" content="WordPress 2.8">
<script type="text/javascript" src="./reflection_files/jquery.js.Download"></script>
<script type="text/javascript" src="./reflection_files/jquery.ifixpng.js.Download"></script>
<script type="text/javascript">jQuery.noConflict();</script>
<style type="text/css">
.entry img, .entry embed, .entry object { max-width: 880px; width: expression(this.clientWidth > 880 ? 880 : true); }
.entry img { height: auto; }</style>
</head>
<body><!-- BEGIN WAYBACK TOOLBAR INSERT -->
<script>__wm.rw(0);</script>
<div id="wm-ipp-base" lang="en" style="display: block; direction: ltr;"><template shadowrootmode="closed"><div id="wm-ipp" style="position:fixed;left:0;top:0;right:0;" class="">
<div id="donato" style="position:relative;width:100%;">
<div id="donato-base">
<iframe id="donato-if" src="https://archive.org/includes/donate.php?as_page=1&amp;platform=wb&amp;referer=https%3A//web.archive.org/web/20090629065657/http%3A//blog.aztec-project.org/2009/06/24/wozu-dient-reflection/" scrolling="no" frameborder="0" style="width:100%; height:100%">
</iframe>
</div>
</div><div id="wm-ipp-inside">
<div id="wm-toolbar" style="position:relative;display:flex;flex-flow:row nowrap;justify-content:space-between;">
<div id="wm-logo" style="/*width:110px;*/padding-top:12px;">
<a href="https://web.archive.org/web/" title="Wayback Machine home page"><img src="https://web.archive.org/_static/images/toolbar/wayback-toolbar-logo-200.png" srcset="/_static/images/toolbar/wayback-toolbar-logo-100.png, /_static/images/toolbar/wayback-toolbar-logo-150.png 1.5x, /_static/images/toolbar/wayback-toolbar-logo-200.png 2x" alt="Wayback Machine" style="width:100px" border="0"></a>
</div>
<div class="c" style="display:flex;flex-flow:column nowrap;justify-content:space-between;flex:1;">
<form class="u" style="display:flex;flex-direction:row;flex-wrap:nowrap;" target="_top" method="get" action="https://web.archive.org/web/submit" name="wmtb" id="wmtb"><input type="text" name="url" id="wmtbURL" value="http://blog.aztec-project.org/2009/06/24/wozu-dient-reflection/" onfocus="this.focus();this.select();" style="flex:1;" autocomplete="off"><input type="hidden" name="type" value="replay"><input type="hidden" name="date" value="20090629065657"><input type="submit" value="Go">
</form>
<div style="display:flex;flex-flow:row nowrap;align-items:flex-end;">
<div class="s" id="wm-nav-captures" style="flex:1;"><a class="t" href="https://web.archive.org/web/*/http://blog.aztec-project.org/2009/06/24/wozu-dient-reflection/" title="See a list of every capture for this URL">9 captures</a><div class="r" title="Timespan for captures of this URL">29 Jun 2009 - 1 Mar 2021</div></div>
<div class="k">
<a href="https://web.archive.org/web/20040301000000/http://blog.aztec-project.org/2009/06/24/wozu-dient-reflection/" id="wm-graph-anchor">
<div id="wm-ipp-sparkline" title="Explore captures for this URL" style="position: relative">
<canvas id="wm-sparkline-canvas" width="750" height="27" border="0"></canvas>
<div class="yt" style="display: none; width: 25px; height: 27px; left: 200px;"></div><div class="mt" style="display: none; width: 2px; height: 27px; left: 205px;"></div></div>
</a>
</div>
</div>
</div>
<div class="n">
<table>
<tbody>
<!-- NEXT/PREV MONTH NAV AND MONTH INDICATOR -->
<tr class="m">
<td class="b" nowrap="nowrap">May</td>
<td class="c" id="displayMonthEl" title="You are here: 06:56:57 Jun 29, 2009">Jun</td>
<td class="f" nowrap="nowrap"><a href="https://web.archive.org/web/20120917014223/http://blog.aztec-project.org:80/2009/06/24/wozu-dient-reflection" title="17 Sep 2012"><strong>Sep</strong></a></td>
</tr>
<!-- NEXT/PREV CAPTURE NAV AND DAY OF MONTH INDICATOR -->
<tr class="d">
<td class="b" nowrap="nowrap"><img src="https://web.archive.org/_static/images/toolbar/wm_tb_prv_off.png" alt="Previous capture" width="14" height="16" border="0"></td>
<td class="c" id="displayDayEl" style="width:34px;font-size:22px;white-space:nowrap;" title="You are here: 06:56:57 Jun 29, 2009">29</td>
<td class="f" nowrap="nowrap"><a href="https://web.archive.org/web/20120917014223/http://blog.aztec-project.org:80/2009/06/24/wozu-dient-reflection" title="01:42:23 Sep 17, 2012"><img src="https://web.archive.org/_static/images/toolbar/wm_tb_nxt_on.png" alt="Next capture" width="14" height="16" border="0"></a></td>
</tr>
<!-- NEXT/PREV YEAR NAV AND YEAR INDICATOR -->
<tr class="y">
<td class="b" nowrap="nowrap">2008</td>
<td class="c" id="displayYearEl" title="You are here: 06:56:57 Jun 29, 2009">2009</td>
<td class="f" nowrap="nowrap"><a href="https://web.archive.org/web/20120917014223/http://blog.aztec-project.org:80/2009/06/24/wozu-dient-reflection" title="17 Sep 2012"><strong>2012</strong></a></td>
</tr>
</tbody>
</table>
</div>
<div class="r" style="display:flex;flex-flow:column nowrap;align-items:flex-end;justify-content:space-between;">
<div id="wm-btns" style="text-align:right;height:23px;">
<span class="xxs">
<div id="wm-save-snapshot-success">success</div>
<div id="wm-save-snapshot-fail">fail</div>
<a id="wm-save-snapshot-open" href="https://web.archive.org/web/20090629065657/http://blog.aztec-project.org/2009/06/24/wozu-dient-reflection/#" title="Share via My Web Archive" style="display: none;">
<span class="iconochive-web"></span>
</a>
<a href="https://archive.org/account/login.php" title="Sign In" id="wm-sign-in" style="display: inline-block;">
<span class="iconochive-person"></span>
</a>
<span id="wm-save-snapshot-in-progress" class="iconochive-web" style="display: none;"></span>
</span>
<a class="xxs" href="http://faq.web.archive.org/" title="Get some help using the Wayback Machine" style="top:-6px;"><span class="iconochive-question" style="color:rgb(87,186,244);font-size:160%;"></span></a>
<a id="wm-tb-close" href="https://web.archive.org/web/20090629065657/http://blog.aztec-project.org/2009/06/24/wozu-dient-reflection/#close" style="top:-2px;" title="Close the toolbar"><span class="iconochive-remove-circle" style="color:#888888;font-size:240%;"></span></a>
</div>
<div id="wm-share" class="xxs">
<a href="https://web.archive.org/web/20090629065657/http://web.archive.org/screenshot/http://blog.aztec-project.org/2009/06/24/wozu-dient-reflection/" id="wm-screenshot" title="screenshot" style="visibility: hidden;">
<span class="wm-icon-screen-shot"></span>
</a>
<a href="https://web.archive.org/web/20090629065657/http://blog.aztec-project.org/2009/06/24/wozu-dient-reflection/#" id="wm-video" title="video">
<span class="iconochive-movies"></span>
</a>
<a id="wm-share-facebook" href="https://web.archive.org/web/20090629065657/http://blog.aztec-project.org/2009/06/24/wozu-dient-reflection/#" data-url="https://web.archive.org/web/20090629065657/http://blog.aztec-project.org:80/2009/06/24/wozu-dient-reflection/" title="Share on Facebook" style="margin-right:5px;" target="_blank"><span class="iconochive-facebook" style="color:#3b5998;font-size:160%;"></span></a>
<a id="wm-share-twitter" href="https://web.archive.org/web/20090629065657/http://blog.aztec-project.org/2009/06/24/wozu-dient-reflection/#" data-url="https://web.archive.org/web/20090629065657/http://blog.aztec-project.org:80/2009/06/24/wozu-dient-reflection/" title="Share on Twitter" style="margin-right:5px;" target="_blank"><span class="iconochive-twitter" style="color:#1dcaff;font-size:160%;"></span></a>
</div>
<div style="padding-right:2px;text-align:right;white-space:nowrap;">
<a id="wm-expand" class="wm-btn wm-closed" href="https://web.archive.org/web/20090629065657/http://blog.aztec-project.org/2009/06/24/wozu-dient-reflection/#expand"><span id="wm-expand-icon" class="iconochive-down-solid"></span> <span class="xxs" style="font-size:80%;">About this capture</span></a>
</div>
</div>
</div>
<div id="wm-capinfo" style="border-top:1px solid #777;display:none; overflow: hidden">
<div id="wm-capinfo-notice" source="api"></div>
<div id="wm-capinfo-collected-by">
<div style="background-color:#666;color:#fff;font-weight:bold;text-align:center">COLLECTED BY</div>
<div style="padding:3px;position:relative" id="wm-collected-by-content">
<div style="display:inline-block;vertical-align:top;width:50%;">
<span class="c-logo" style="background-image:url(https://archive.org/services/img/alexacrawls);"></span>
Organization: <a style="color:#33f;" href="https://archive.org/details/alexacrawls" target="_new"><span class="wm-title">Alexa Crawls</span></a>
<div style="max-height:75px;overflow:hidden;position:relative;">
<div style="position:absolute;top:0;left:0;width:100%;height:75px;background:linear-gradient(to bottom,rgba(255,255,255,0) 0%,rgba(255,255,255,0) 90%,rgba(255,255,255,255) 100%);"></div>
Starting in 1996, <a href="http://www.alexa.com/">Alexa Internet</a> has been donating their crawl data to the Internet Archive. Flowing in every day, these data are added to the <a href="http://web.archive.org/">Wayback Machine</a> after an embargo period.
</div>
</div>
<div style="display:inline-block;vertical-align:top;width:49%;">
<span class="c-logo" style="background-image:url(https://archive.org/services/img/alexa_web_2009)"></span>
<div>Collection: <a style="color:#33f;" href="https://archive.org/details/alexa_web_2009" target="_new"><span class="wm-title">alexa_web_2009</span></a></div>
<div style="max-height:75px;overflow:hidden;position:relative;">
<div style="position:absolute;top:0;left:0;width:100%;height:75px;background:linear-gradient(to bottom,rgba(255,255,255,0) 0%,rgba(255,255,255,0) 90%,rgba(255,255,255,255) 100%);"></div>
this data is currently not publicly accessible.
</div>
</div>
</div>
</div>
<div id="wm-capinfo-timestamps">
<div style="background-color:#666;color:#fff;font-weight:bold;text-align:center" title="Timestamps for the elements of this page">TIMESTAMPS</div>
<div>
<div id="wm-capresources" style="margin:0 5px 5px 5px;max-height:250px;overflow-y:scroll !important"></div>
<div id="wm-capresources-loading" style="text-align:left;margin:0 20px 5px 5px;display:none"><img src="https://web.archive.org/_static/images/loading.gif" alt="loading"></div>
</div>
</div>
</div></div></div><link rel="stylesheet" type="text/css" href="./reflection_files/banner-styles.css"><link rel="stylesheet" type="text/css" href="./reflection_files/iconochive.css"><div class="wb-autocomplete-suggestions "></div></template>
</div><div id="wm-ipp-print">The Wayback Machine - https://web.archive.org/web/20090629065657/http://blog.aztec-project.org:80/2009/06/24/wozu-dient-reflection/</div>
<script type="text/javascript">//<![CDATA[
__wm.bt(750,27,25,2,"web","http://blog.aztec-project.org/2009/06/24/wozu-dient-reflection/","20090629065657",1996,"/_static/",["/_static/css/banner-styles.css?v=S1zqJCYt","/_static/css/iconochive.css?v=3PDvdIFv"], false);
__wm.rw(1);
//]]></script>
<!-- END WAYBACK TOOLBAR INSERT -->
<div id="page">
<div id="header" class="png">
<div id="headerimg">
<h1 class="blogname" id="blogname"><a href="https://web.archive.org/web/20090629065657/http://blog.aztec-project.org/">build broken</a></h1><span class="blogname text-stroke-br">build broken</span><span class="blogname text-stroke-bl">build broken</span><span class="blogname text-stroke-tr">build broken</span><span class="blogname text-stroke-tl">build broken</span>
<h6 class="blogdesc" id="blogdesc">we should think of a subtitle :)</h6><span class="blogdesc text-stroke-br">we should think of a subtitle :)</span><span class="blogdesc text-stroke-bl">we should think of a subtitle :)</span><span class="blogdesc text-stroke-tr">we should think of a subtitle :)</span><span class="blogdesc text-stroke-tl">we should think of a subtitle :)</span>
<ul class="pages png head_overlay">
<li class="page_item"><a href="https://web.archive.org/web/20090629065657/http://blog.aztec-project.org/">Home</a></li>
<li class="page_item page-item-2"><a href="https://web.archive.org/web/20090629065657/http://blog.aztec-project.org/about/" title="About">About</a></li>
</ul>
</div>
</div>
<div id="main" class="png">
<table>
<tbody><tr>
<td id="content" class="widecolumn">
<div class="navigation">
<div class="alignleft">«&nbsp;<a href="https://web.archive.org/web/20090629065657/http://blog.aztec-project.org/2009/06/19/net-webservice-mit-asp-classic-ansprechen/">.NET-Webservice mit ASP-Classic ansprechen</a></div>
<div class="alignright"></div>
</div>
<div class="post" id="post-280">
<div class="postinfo">
<div class="datestamp">
<div>
<span class="cal1 cal1x">Jun</span>
<span class="cal2">24</span>
<span class="cal3">2009</span>
</div>
</div>
<span class="switch-post">
<a href="javascript:toggleSidebars();" class="switch-sidebars"><img src="./reflection_files/bullet_sidebars_hide.png" alt="" class="png"></a><a href="javascript:togglePost(280);" id="switch-post-280"><img src="./reflection_files/bullet_toggle_minus.png" alt="" class="png"></a>
</span>
<h2 class="posttitle"><a href="https://web.archive.org/web/20090629065657/http://blog.aztec-project.org/2009/06/24/wozu-dient-reflection/" rel="bookmark" title="Permanent Link: Wozu dient Reflection">Wozu dient Reflection</a></h2>
<small>
Geschrieben von <a href="https://web.archive.org/web/20090629065657/http://blog.aztec-project.org/author/tch/" title="Beiträge von Thomas Christian">Thomas Christian</a> in <a href="https://web.archive.org/web/20090629065657/http://blog.aztec-project.org/category/how-to/" title="Alle Artikel in How-To anzeigen" rel="category tag">How-To</a>, tags: <a href="https://web.archive.org/web/20090629065657/http://blog.aztec-project.org/tag/assemblies/" rel="tag">Assemblies</a>, <a href="https://web.archive.org/web/20090629065657/http://blog.aztec-project.org/tag/assemby/" rel="tag">Assemby</a>, <a href="https://web.archive.org/web/20090629065657/http://blog.aztec-project.org/tag/c/" rel="tag">C#</a>, <a href="https://web.archive.org/web/20090629065657/http://blog.aztec-project.org/tag/reflection/" rel="tag">Reflection</a> </small>
</div>
<div class="entry">
<h2><span style="color: #ffffff;">.</span></h2>
<h2>Vorbereitung</h2>
<p>Über Reflection kann man zur Laufzeit relativ einfach an alle Informationen eines Assembly herankommen. So ist es möglich, dass man von einem Assembly den Namen, Klassen, Methoden mit ihren Parametern, Eigenschaften und Rückgabewerte, usw. auslesen kann. Aber nicht nur das Auslesen ist möglich, sondern auch das Setzen.</p>
<p>Jetzt stellt sich für den einen oder anderen vielleicht die Frage, warum man zur Laufzeit auf diese Daten zugreifen kann. Jede Assembly verfügt über Module in denen sich die einzelnen Typen(Klassen) befinden. Für jedes Modul gibt es Metadaten, in denen die einzelnen Typen beschrieben werden. Die Assembly selbst verfügt noch über ein Manifest, in dem sich alle Informationen zum Assembly (z.B. Name, Version) befinden. Wenn man über Reflection auf Assemblies zugreift, dann werden diese Metadaten und das Manifest dafür herangezogen. Die Metadaten verfügen zwar noch über die Informationen, welche Modifizierer für Methoden, Properties, usw. verwendet wurden, aber der MSIL-Code (die Zwischensprache, die vom Endcompiler in die plattformspezifische Sprache übersetzt wird) nicht mehr. Die Modifizierer sind nur für den Compiler relevant und werden nach dem Kompilieren nicht mehr berücksichtigt. Somit ist es auch möglich über Reflection Properties, Methoden, usw. zu verwenden, die nicht öffentlich sind.</p>
<p>Für dieses Beispiel habe ich eine kleine Konsolen-Anwendung geschrieben (TestApplication), welche eine Referenz auf das Assembly TestProject.dll hat. Dieses Assembly hat zwei von mir erstellte Typen (Klassen) TestClass und SecondTestClass. In den beiden Klassen steckt keine Logik. Sie sind ausschließlich für dieses Beispiel erstellt worden, um ein paar Daten zu modifizieren und um zu zeigen wie die Informationen aus dem Assembly ausgelesen und verändert werden können und dass es möglich ist an nicht-öffentliche Methoden heranzukommen.</p>
<p><span style="text-decoration: underline;"><strong>TestClass.cs</strong></span></p>
<div style="background: white none repeat scroll 0% 0%; font-family: Courier New; font-size: 10pt; color: black;">
<pre style="margin: 0px;"><span style="color: blue;">using</span> System.Security.Cryptography;</pre>
<pre style="margin: 0px;"><span style="color: blue;">using</span> System.Text;</pre>
<pre style="margin: 0px;"><span style="color: blue;">namespace</span> TestProject {</pre>
<pre style="margin: 0px;"><span style="color: blue;">class</span> <span style="color: #2b91af;">TestClass</span> {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> Name { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> Surname { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">string</span> Password { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> GetFullName() {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: blue;">string</span>.Format(<span style="color: #a31515;">"{0} {1}"</span>, Name ?? <span style="color: blue;">string</span>.Empty, Surname ?? <span style="color: blue;">string</span>.Empty).Trim();</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> SetPasswort(<span style="color: blue;">string</span> password){</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Password = password;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: blue;">string</span> CalcPassword(<span style="color: blue;">string</span> password){</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">MD5</span> md5 = <span style="color: #2b91af;">MD5</span>.Create();</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">byte</span>[] data = md5.ComputeHash(<span style="color: #2b91af;">Encoding</span>.Default.GetBytes(password));</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">StringBuilder</span> hashPassword = <span style="color: blue;">new</span> <span style="color: #2b91af;">StringBuilder</span>();</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">foreach</span> (<span style="color: blue;">byte</span> b <span style="color: blue;">in</span> data){</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; hashPassword.Append(b);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> hashPassword.ToString();</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: blue;">string</span> DoSomething(<span style="color: blue;">string</span> value) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: green;">// do something</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> value;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">}</pre>
</div>
<p><span style="text-decoration: underline;"><strong>SecondTestClass.cs</strong></span></p>
<div style="background: white none repeat scroll 0% 0%; font-family: Courier New; font-size: 10pt; color: black;">
<pre style="margin: 0px;"><span style="color: blue;">namespace</span> TestProject {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">SecondTestClass</span> {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">int</span> Addition(<span style="color: blue;">int</span> a, <span style="color: blue;">int</span> b) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> a + b;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">int</span> Multiply(<span style="color: blue;">int</span> a, <span style="color: blue;">int</span> b) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> a * b;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">double</span> Divide(<span style="color: blue;">int</span> a, <span style="color: blue;">int</span> b) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (b == 0) <span style="color: blue;">return</span> 0;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> a / b;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">}
<span style="color: #ffffff;">.
.</span></pre>
</div>
<h2>Auslesen von Assembly- Type-Informationen</h2>
<p>Mit der Methode Thread.GetDomain().GetAssemblies() bekommt man alle referenzierten Assemblies der aktuellen Domäne. Damit die TestProject-Assembly auch in dieser Domäne vorhanden ist, muss vor dem Aufruf dieser Methode eine Instanz von einem Objekt dieses Assemblies erzeugt werden.</p>
<p>Um sich nun alle Assemblies der aktuellen Domäne anzeigen zu lassen, könnte man folgendes schreiben:</p>
<div style="background: white none repeat scroll 0% 0%; font-family: Courier New; font-size: 10pt; color: black;">
<pre style="margin: 0px;"><span style="color: blue;">static</span> <span style="color: blue;">void</span> Main(<span style="color: blue;">string</span>[] args) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">SecondTestClass</span> secondTestClass = <span style="color: blue;">new</span> <span style="color: #2b91af;">SecondTest</span><span style="color: #2b91af;">&nbsp;</span><span style="color: #2b91af;">Class</span>();</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assembly</span>[] assemblies = <span style="color: #2b91af;">Thread</span>.GetDomain().GetAssemblies();</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">foreach</span> (<span style="color: blue;">var</span> assembly <span style="color: blue;">in</span> assemblies) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Console</span>.WriteLine(<span style="color: blue;">string</span>.Format(<span style="color: #a31515;">"-- {0} ver.: {1}"</span>, assembly.GetName().Name
, assembly.GetName().Version));</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Console</span>.ReadLine();</pre>
<pre style="margin: 0px;">}
<img class="size-full wp-image-281 aligncenter" src="./reflection_files/1.png" alt="1" width="549" height="120"></pre>
</div>
<p><span style="color: #ffffff;">.</span></p>
<p>Nun könnte man noch alle zur Verfügung stehenden Typen(Klassen) aus der TestProject-Assembly anzeigen.</p>
<div style="background: white none repeat scroll 0% 0%; font-family: Courier New; font-size: 10pt; color: black;">
<div style="background: white none repeat scroll 0% 0%; font-family: Courier New; font-size: 10pt; color: black;">
<pre style="margin: 0px;"><span style="color: blue;">static</span> <span style="color: blue;">void</span> Main(<span style="color: blue;">string</span>[] args) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">SecondTestClass</span> secondTestClass = <span style="color: blue;">new</span> <span style="color: #2b91af;">SecondTestClass</span>();</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assembly</span>[] assemblies = <span style="color: #2b91af;">Thread</span>.GetDomain().GetAssemblies();</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">foreach</span> (<span style="color: blue;">var</span> assembly <span style="color: blue;">in</span> assemblies) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Console</span>.WriteLine(<span style="color: blue;">string</span>.Format(<span style="color: #a31515;">"-- {0} ver.: {1}"</span>, assembly.GetName().Name
<pre style="margin: 0px;"> , assembly.GetName().Version));</pre>
</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (assembly.GetName().Name.Equals(<span style="color: #a31515;">"TestProject"</span>)) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Type</span>[] types = assembly.GetTypes();</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">foreach</span> (<span style="color: blue;">var</span> type <span style="color: blue;">in</span> types) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Console</span>.WriteLine(<span style="color: #a31515;">"&nbsp; ++{0}"</span>, type.Name);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Console</span>.ReadLine();</pre>
<pre style="margin: 0px; text-align: left;">}
<img class="size-full wp-image-284 aligncenter" title="2" src="./reflection_files/2.png" alt="2" width="533" height="130"></pre>
</div>
</div>
<p><span style="color: #ffffff;">.</span></p>
<p>Wenn man nun allerdings Informationen eines bestimmten Types haben möchte, so werden diese Informationen aus den Metadaten des Moduls geladen, in dem sich der Type befindet. Um zum Beispiel alle öffentlichen Methoden eines Types anzuzeigen, könnte man folgendes schreiben:</p>
<div style="background: white none repeat scroll 0% 0%; font-family: Courier New; font-size: 10pt; color: black;">
<pre style="margin: 0px;"><span style="color: blue;">static</span> <span style="color: blue;">void</span> Main(<span style="color: blue;">string</span>[] args) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">SecondTestClass</span> secondTestClass = <span style="color: blue;">new</span> <span style="color: #2b91af;">SecondTestClass</span>();</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assembly</span>[] assemblies = <span style="color: #2b91af;">Thread</span>.GetDomain().GetAssemblies();</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">foreach</span> (<span style="color: blue;">var</span> assembly <span style="color: blue;">in</span> assemblies) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Console</span>.WriteLine(<span style="color: blue;">string</span>.Format(<span style="color: #a31515;">"-- {0} ver.: {1}"</span>, assembly.GetName().Name
, assembly.GetName().Version));</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (assembly.GetName().Name.Equals(<span style="color: #a31515;">"TestProject"</span>)) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Type</span>[] types = assembly.GetTypes();</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">foreach</span> (<span style="color: blue;">var</span> type <span style="color: blue;">in</span> types) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Console</span>.WriteLine(<span style="color: #a31515;">"&nbsp; ++ {0}"</span>, type.Name);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">MethodInfo</span>[] methodInfos = type.GetMethods();</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">foreach</span> (<span style="color: blue;">var</span> info <span style="color: blue;">in</span> methodInfos) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Console</span>.WriteLine(<span style="color: #a31515;">"&nbsp;&nbsp;&nbsp; ++ {0}"</span>, info.Name);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Console</span>.ReadLine();</pre>
<pre style="margin: 0px;">}</pre>
</div>
<p align="center">
</p><p style="text-align: center;"><img class="size-full wp-image-285 aligncenter" title="3" src="./reflection_files/3.png" alt="3" width="629" height="383"></p>
<p><span style="color: #ffffff;">.</span></p>
<p>Es fällt auf, dass auch die Methoden ToString, Equals, GetHashCode und GetType in beiden Klassen angezeigt werden, obwohl diese in den Klassen überhaupt nicht deklariert wurden. Das liegt daran, dass alle Typen von <span style="color: blue;">object </span>ableiten und <span style="color: blue;">object</span> diese Methoden implementiert. Was noch auffällt ist, dass nur öffentliche Methoden angezeigt werden. Möchte man sich auch die privaten Methoden anzeigen lassen, so muss man der Methode type.GetMethods Parameter übergeben. Diese Methode verlangt BindingFlags, die man mit dem Bitweise ODER-Operator (|) miteinander verknüpfen kann. Um sich nun öffentliche und private Methoden anzeigen zu lassen, muss der Aufruf der Methode type.GetMethods folgendermaßen lauten:</p>
<div style="background: white none repeat scroll 0% 0%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
<pre style="margin: 0px;"><span style="color: #2b91af;">MethodInfo</span>[] methodInfos = type.GetMethods(<span style="color: #2b91af;">BindingFlags</span>.Public | <span style="color: #2b91af;">BindingFlags</span>.NonPublic | <span style="color: #2b91af;">BindingFlags</span>.Instance);</pre>
</div>
<p>Dieser Aufruf gibt an, dass man alle öffentlichen und nicht-öffentlichen Instanz-Methoden bekommen möchte. Instanz-Methoden bedeutet, dass es sich um Methoden handelt, die nur mit dem Instanzieren eines Objektes zur Verfügung stehen. D.h. es werden keine static-Methoden angezeigt. Es gibt noch weitere BindingFlags, auf die ich hier allerdings nicht weiter eingehen möchte.</p>
<p>Das ganze könnte man natürlich noch in der Form weiter treiben, dass man sich die kompletten Methodensignaturen mit Modifizierer, Schlüsselwort und Rückgabewert anzeigen lässt:</p>
<div style="background: white none repeat scroll 0% 0%; font-family: Courier New; font-size: 10pt; color: black;">
<pre style="margin: 0px;"><span style="color: blue;">static</span> <span style="color: blue;">void</span> Main(<span style="color: blue;">string</span>[] args) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">SecondTestClass</span> secondTestClass = <span style="color: blue;">new</span> <span style="color: #2b91af;">SecondTestClass</span>();</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assembly</span>[] assemblies = <span style="color: #2b91af;">Thread</span>.GetDomain().GetAssemblies();</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">foreach</span> (<span style="color: blue;">var</span> assembly <span style="color: blue;">in</span> assemblies) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Console</span>.WriteLine(<span style="color: blue;">string</span>.Format(<span style="color: #a31515;">"-- {0} ver.: {1}"</span>, assembly.GetName().Name, assembly.GetName().Version));</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (assembly.GetName().Name.Equals(<span style="color: #a31515;">"TestProject"</span>)) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Type</span>[] types = assembly.GetTypes();</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">foreach</span> (<span style="color: blue;">var</span> type <span style="color: blue;">in</span> types) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Console</span>.WriteLine(<span style="color: #a31515;">"&nbsp; ++ {0}"</span>, type.Name);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">MethodInfo</span>[] methodInfos = type.GetMethods(<span style="color: #2b91af;">BindingFlags</span>.Public | <span style="color: #2b91af;">BindingFlags</span>.NonPublic
| <span style="color: #2b91af;">BindingFlags</span>.Instance
<pre style="margin: 0px;"> | <span style="color: #2b91af;">BindingFlags</span>.Static);</pre>
</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">foreach</span> (<span style="color: blue;">var</span> info <span style="color: blue;">in</span> methodInfos) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">string</span> modifier = GetModifier(info);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">string</span> keyword = GetKeyword(info);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">string</span> parameters = GetParameters(info);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (!<span style="color: blue;">string</span>.IsNullOrEmpty(keyword)) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; keyword = <span style="color: blue;">string</span>.Format(<span style="color: #a31515;">"{0} "</span>, keyword);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Console</span>.WriteLine(<span style="color: #a31515;">"&nbsp;&nbsp;&nbsp; ++ {0} {1}{2}{3}({4})"</span>, modifier, keyword
, info.ReturnParameter, info.Name, parameters);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Console</span>.ReadLine();</pre>
<pre style="margin: 0px;">}</pre>
<pre style="margin: 0px;"><span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: blue;">string</span> GetModifier(<span style="color: #2b91af;">MethodInfo</span> info) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (info.IsPublic) <span style="color: blue;">return</span> <span style="color: #a31515;">"public"</span>;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: #a31515;">"private"</span>;</pre>
<pre style="margin: 0px;">}</pre>
<pre style="margin: 0px;"><span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: blue;">string</span> GetKeyword(<span style="color: #2b91af;">MethodInfo</span> info) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (info.IsStatic) <span style="color: blue;">return</span> <span style="color: #a31515;">"static"</span>;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (info.IsVirtual) <span style="color: blue;">return</span> <span style="color: #a31515;">"virtual"</span>;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: blue;">string</span>.Empty;</pre>
<pre style="margin: 0px;">}</pre>
<pre style="margin: 0px;"><span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: blue;">string</span> GetParameters(<span style="color: #2b91af;">MethodInfo</span> info) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">StringBuilder</span> parameters = <span style="color: blue;">new</span> <span style="color: #2b91af;">StringBuilder</span>();</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">ParameterInfo</span>[] parameterInfos = info.GetParameters();</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (parameterInfos != <span style="color: blue;">null</span>) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">foreach</span> (<span style="color: #2b91af;">ParameterInfo</span> parameterInfo <span style="color: blue;">in</span> parameterInfos) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (!<span style="color: blue;">string</span>.IsNullOrEmpty(parameters.ToString())) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; parameters.Append(<span style="color: #a31515;">", "</span>);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; parameters.AppendFormat(<span style="color: #a31515;">"{0} {1}"</span>, parameterInfo.ParameterType.Name, parameterInfo.Name);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> parameters.ToString();</pre>
<pre style="margin: 0px;">}</pre>
</div>
<p style="text-align: center;"><img class="size-full wp-image-287 aligncenter" title="4" src="./reflection_files/4.png" alt="4" width="605" height="443"></p>
<p><span style="color: #ffffff;">.</span></p>
<p>Nicht immer ist bereits während der Entwicklung bekannt, aus welchem Assembly man Informationen benötigt. Sodass die Methode Thread.GetDomain().GetAssemblies() nicht verwendet werden kann. Eine weitere Möglichkeit an ein Assembly zu kommen ist es direkt zu laden. Dafür bietet die Klasse Assembly die statische Methode Assembly.LoadFrom zum Laden von Assemblies. Statt dem Laden des Assembly über die aktuelle Domäne, wird das nächste Beispiel so modifiziert, dass die TestProject-Assembly direkt aus einem Verzeichnis heraus geladen wird.</p>
<div style="background: white none repeat scroll 0% 0%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
<pre style="margin: 0px;"><span style="color: blue;">static</span> <span style="color: blue;">void</span> Main(<span style="color: blue;">string</span>[] args) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assembly</span> assembly = <span style="color: #2b91af;">Assembly</span>.LoadFrom(<span style="color: #a31515;">@"c:\TestProject.dll"</span>);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (assembly != <span style="color: blue;">null</span>) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Console</span>.WriteLine(<span style="color: blue;">string</span>.Format(<span style="color: #a31515;">"-- {0} ver.: {1}"</span>, assembly.GetName().Name, assembly.GetName().Version));</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Type</span>[] types = assembly.GetTypes();</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">foreach</span> (<span style="color: blue;">var</span> type <span style="color: blue;">in</span> types) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Console</span>.WriteLine(<span style="color: #a31515;">"&nbsp; ++ {0}"</span>, type.Name);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">MethodInfo</span>[] methodInfos =</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; type.GetMethods(<span style="color: #2b91af;">BindingFlags</span>.Public | <span style="color: #2b91af;">BindingFlags</span>.NonPublic | <span style="color: #2b91af;">BindingFlags</span>.Instance |</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">BindingFlags</span>.Static);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">foreach</span> (<span style="color: blue;">var</span> info <span style="color: blue;">in</span> methodInfos) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">string</span> modifier = GetModifier(info);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">string</span> keyword = GetKeyword(info);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">string</span> parameters = GetParameters(info);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (!<span style="color: blue;">string</span>.IsNullOrEmpty(keyword)) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; keyword = <span style="color: blue;">string</span>.Format(<span style="color: #a31515;">"{0} "</span>, keyword);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Console</span>.WriteLine(<span style="color: #a31515;">"&nbsp;&nbsp;&nbsp; ++ {0} {1}{2}{3}({4})"</span>, modifier, keyword, info.ReturnParameter,</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; info.Name, parameters);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Console</span>.ReadLine();</pre>
<pre style="margin: 0px;">}</pre>
</div>
<p align="center">
</p><div id="attachment_288" class="wp-caption aligncenter" style="width: 687px"><img class="size-full wp-image-288" title="5" src="./reflection_files/5.png" alt="5" width="677" height="419"><p class="wp-caption-text">Man sieht, dass die gleiche Ausgabe wie bereits im vorherigen Beispiel generiert wird.</p></div>
<p style="text-align: center;">
</p><h2><span style="color: #ffffff;">.</span></h2>
<h2>Interaktionen</h2>
<p>Nachdem die Vorgehensweise nun klar sein sollte, möchte ich nun den eigentlich interessanten Teil demonstrieren. Wie kann man nun auf Methoden usw. zugreifen und wie können Werte aus einem Type gelesen und veränder werden? Wenn man die Methode SecondTestClass.Addition(int a, int b) aufrufen möchte, dann braucht man die Methodeninformationen von dem Type SecondTestClass aus dem TestProject-Assembly. D.h. man muss wie in den obigen Beispielen auch über alle Typen iterieren, um an den gesuchten Type SecondTestClass zu kommen. Hat man den gesuchten Type gefunden, so kann man sich von diesem alle Methoden in einem Array zurückgeben lassen. Dieses Array kann dann wieder ganz normal durchlaufen und nach der gewünschten Methode durchsucht werden. Hat man die Methode gefunden so muss zuerst eine Instanz von dem Objekt erzeugt werden und dafür gibt es den Activator, der die statische Methode CreateInstance beinhaltet. Als Parameter übergibt man ihr den Type von dem eine Instanz erzeugt werden soll. Jetzt kann man über die MethodInfo die Methode aufrufen. Dazu muss der Methode die Instanz des Objektes übergeben werden, von welchem die Methode aufgerufen werden soll und zusätzlich noch ein Array von Objekten, welche die zu übergebenen Parameter beinhaltet. Hat die Methode keine Parameter, so muss null übergeben werden. Dabei muss der Rückgabewert in den erwarteten Wert gecastet werden. Das ganze könnte dann folgendermaßen aussehen:</p>
<div style="background: white none repeat scroll 0% 0%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
<pre style="margin: 0px;"><span style="color: blue;">static</span> <span style="color: blue;">void</span> Main(<span style="color: blue;">string</span>[] args) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assembly</span> assembly = <span style="color: #2b91af;">Assembly</span>.LoadFrom(<span style="color: #a31515;">@"c:\TestProject.dll"</span>);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (assembly != <span style="color: blue;">null</span>) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Console</span>.WriteLine(<span style="color: blue;">string</span>.Format(<span style="color: #a31515;">"-- {0} ver.: {1}"</span>, assembly.GetName().Name, assembly.GetName().Version));</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Type</span>[] types = assembly.GetTypes();</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">foreach</span> (<span style="color: blue;">var</span> type <span style="color: blue;">in</span> types) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (type.Name.Equals(<span style="color: #a31515;">"SecondTestClass"</span>)) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Console</span>.WriteLine(<span style="color: #a31515;">"&nbsp; ++ {0}"</span>, type.Name);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">MethodInfo</span>[] methodInfos = type.GetMethods();</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">foreach</span> (<span style="color: blue;">var</span> info <span style="color: blue;">in</span> methodInfos) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (info.Name.Equals(<span style="color: #a31515;">"Addition"</span>)) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">object</span> obj = <span style="color: #2b91af;">Activator</span>.CreateInstance(type);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">int</span> value = (<span style="color: blue;">int</span>)info.Invoke(obj, <span style="color: blue;">new</span> <span style="color: blue;">object</span>[] { 2, 5 });</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Console</span>.WriteLine(<span style="color: #a31515;">"&nbsp;&nbsp;&nbsp; ++ {0}(2 + 5) = {1}"</span>, info.Name, value);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Console</span>.ReadLine();</pre>
<pre style="margin: 0px;">}</pre>
</div>
<p align="center">
</p><div id="attachment_289" class="wp-caption aligncenter" style="width: 543px"><img class="size-full wp-image-289" title="6" src="./reflection_files/6.png" alt="6" width="533" height="143"><p class="wp-caption-text">Aufruf der Methode Addition</p></div>
<p style="text-align: center;">
</p><p><span style="color: #ffffff;">.</span></p>
<p>Bei statischen Methoden kann das Erzeugen der Instanz weggelassen werden und für den Parameter des Instanz-Objektes kann null übergeben werden.</p>
<div style="background: white none repeat scroll 0% 0%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
<pre style="margin: 0px;"><span style="color: blue;">if</span> (type.Name.Equals(<span style="color: #a31515;">"TestClass"</span>)) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Console</span>.WriteLine(<span style="color: #a31515;">"&nbsp; ++ {0}"</span>, type.Name);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">MethodInfo</span>[] methodInfos = type.GetMethods(<span style="color: #2b91af;">BindingFlags</span>.NonPublic | <span style="color: #2b91af;">BindingFlags</span>.Static);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">foreach</span> (<span style="color: blue;">var</span> info <span style="color: blue;">in</span> methodInfos) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (info.Name.Equals(<span style="color: #a31515;">"CalcPassword"</span>)) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">string</span> value = (<span style="color: blue;">string</span>)info.Invoke(<span style="color: blue;">null</span>, <span style="color: blue;">new</span> <span style="color: blue;">object</span>[] { <span style="color: #a31515;">"myPassword"</span> });</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Console</span>.WriteLine(<span style="color: #a31515;">"&nbsp;&nbsp;&nbsp; ++ {0}(\"myPassword\") = {1}"</span>, info.Name, value);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">}</pre>
</div>
<p align="center">
</p><div id="attachment_290" class="wp-caption aligncenter" style="width: 687px"><img class="size-full wp-image-290" title="7" src="./reflection_files/7.png" alt="7" width="677" height="155"><p class="wp-caption-text">Das übergebene Passwort wurde über eine statische Methode gehashed.</p></div>
<p style="text-align: center;">
</p><p><span style="color: #ffffff;">.</span></p>
<p>Um den Wert einer Property auszulesen, muss anstelle von MethodInfo die PropertyInfo verwendet werden. Aus dieser kann dann über die Methode PropertyInfo.GetValue der aktuelle Wert zurückgegeben werden. Da in diesem Beispiel nach dem Instanzieren des Objektes noch keine Werte zugewiesen wurden, sind die Properties leer.</p>
<div style="background: white none repeat scroll 0% 0%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
<pre style="margin: 0px;"><span style="color: blue;">static</span> <span style="color: blue;">void</span> Main(<span style="color: blue;">string</span>[] args) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assembly</span> assembly = <span style="color: #2b91af;">Assembly</span>.LoadFrom(<span style="color: #a31515;">@"C:\ TestProject.dll"</span>);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (assembly != <span style="color: blue;">null</span>) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Console</span>.WriteLine(<span style="color: blue;">string</span>.Format(<span style="color: #a31515;">"-- {0} ver.: {1}"</span>, assembly.GetName().Name, assembly.GetName().Version));</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Type</span>[] types = assembly.GetTypes();</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">foreach</span> (<span style="color: blue;">var</span> type <span style="color: blue;">in</span> types) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (type.Name.Equals(<span style="color: #a31515;">"TestClass"</span>)) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Console</span>.WriteLine(<span style="color: #a31515;">"&nbsp; ++ {0}"</span>, type.Name);
<pre style="margin: 0px;"><span style="color: blue;"> object</span> obj = <span style="color: #2b91af;">Activator</span>.CreateInstance(type);</pre>
</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">PropertyInfo</span>[] propertyInfos = type.GetProperties(<span style="color: #2b91af;">BindingFlags</span>.Public | <span style="color: #2b91af;">BindingFlags</span>.NonPublic
| <span style="color: #2b91af;">BindingFlags</span>.Instance);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">foreach</span> (<span style="color: blue;">var</span> info <span style="color: blue;">in</span> propertyInfos) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Console</span>.WriteLine(<span style="color: #a31515;">"&nbsp;&nbsp;&nbsp; ++ {0} = {1}"</span>, info.Name, info.GetValue(obj, <span style="color: blue;">null</span>));</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Console</span>.ReadLine();</pre>
<pre style="margin: 0px;">}
<img class="aligncenter size-full wp-image-373" title="8" src="./reflection_files/8.png" alt="8" width="517" height="203"></pre>
</div>
<p><span style="color: #ffffff;">.</span></p>
<p>Wenn man nun die Properties mit Werten belegen möchte, so muss man analog zur Methode PropertyInfo.GetValue, die Methode PropertyInfo. SetValue verwenden.</p>
<div style="background: white none repeat scroll 0% 0%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
<pre style="margin: 0px;"><span style="color: blue;">if</span> (type.Name.Equals(<span style="color: #a31515;">"TestClass"</span>)) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Console</span>.WriteLine(<span style="color: #a31515;">"&nbsp; ++ {0}"</span>, type.Name);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">PropertyInfo</span>[] propertyInfos = type.GetProperties(<span style="color: #2b91af;">BindingFlags</span>.Public | <span style="color: #2b91af;">BindingFlags</span>.NonPublic
| <span style="color: #2b91af;">BindingFlags</span>.Instance);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; propertyInfos[0].SetValue(obj, <span style="color: #a31515;">"Peter"</span>, <span style="color: blue;">null</span>);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; propertyInfos[1].SetValue(obj, <span style="color: #a31515;">"Müller"</span>, <span style="color: blue;">null</span>);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; propertyInfos[2].SetValue(obj, <span style="color: #a31515;">"myPassword"</span>, <span style="color: blue;">null</span>);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">foreach</span> (<span style="color: blue;">var</span> info <span style="color: blue;">in</span> propertyInfos) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Console</span>.WriteLine(<span style="color: #a31515;">"&nbsp;&nbsp;&nbsp; ++ {0} = {1}"</span>, info.Name, info.GetValue(obj, <span style="color: blue;">null</span>));</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">}
<img class="aligncenter size-full wp-image-374" title="9" src="./reflection_files/9.png" alt="9" width="477" height="167"></pre>
</div>
<p><strong> </strong></p>
<h2><span style="color: #ffffff;">.</span></h2>
<p><span style="color: #ffffff;">.<br>
</span></p>
<h2>Wozu braucht man jetzt aber nun die Reflection?</h2>
<p>Nun ja, die meisten Programme werden Reflection wohl nie benötigen oder verwenden. Es wird vorallem bei dem Entwickeln von Debuggern, Interpretern, TestTools, Logger oder auch für O/R-Mapper verwendet. Es wird also überall dort gebraucht, wo während der Laufzeit nicht klar ist, welche Assemblies verwendet werden und welche Informationen man aus diesen lesen oder manipulieren möchte.</p>
<h2><span style="color: #ffffff;">.</span></h2>
<h2>kurze Zusammenfassung:</h2>
<p>Wie man gesehen hat, ist der Aufbau einer Assembly mit ihren Typen hierarchisch. Um zb. an einen Parameter einer Methode eines Types zu kommen, braucht man zuerst das Assembly welches den Typ enthält. Von diesem lässt man sich ein Array von Typen geben. Von dem gesuchten Typ lässt man sich dann ein Array von Methoden geben. Von der gesuchten Methode holt man sich nun ein Array von allen Parametern. Dieses Array kann dann nach dem gewünschten Parameter durchsucht werden.</p>
<p>Es gibt natürlich für all diese Methoden auch Methoden, mit denen man sich den Type, die Methodeninformation, die Parameterinformation, usw. direkt mit der entsprechenden Bezeichnung holen kann. Ich wollte hier nur auf die Hierarchie aufmerksam machen.</p>
<p>Wie man in diesen Beispielen erkennen konnte, ist es möglich, ohne Probleme auf alle Informationen von einem Assembly zuzgreifen, sich die Types zu laden, dessen öffentliche und auch nicht-öffentliche Werte zu lesen und zu manipulieren und auch Methoden auszuführen. Man kann sich auch lokale Werte anzeigen lassen, in dem man sich den MethodBody des Objekte geben lässt, von denen man die lokalen Werte ermitteln möchte.</p>
<p class="postmetadata alt clear">
<small>
Geschrieben am Mittwoch, 24. Juni 2009 um 12:13 und ist zu finden in <a href="https://web.archive.org/web/20090629065657/http://blog.aztec-project.org/category/how-to/" title="Alle Artikel in How-To anzeigen" rel="category tag">How-To</a>. Du kannst die Antworten zu diesem Thema verfolgen unter <a href="https://web.archive.org/web/20090629065657/http://blog.aztec-project.org/2009/06/24/wozu-dient-reflection/feed/">RSS 2.0</a> feed. Du kannst <a href="https://web.archive.org/web/20090629065657/http://blog.aztec-project.org/2009/06/24/wozu-dient-reflection/#respond">Antwort</a>, oder <a href="https://web.archive.org/web/20090629065657/http://blog.aztec-project.org/2009/06/24/wozu-dient-reflection/trackback/" rel="trackback">trackback</a> von deiner Seite.
</small>
</p>
</div>
</div>
<div id="respond">
<div class="title">Hinterlasse einen Kommentar</div>
<div class="cancel-comment-reply">
<small><a rel="nofollow" id="cancel-comment-reply-link" href="https://web.archive.org/web/20090629065657/http://blog.aztec-project.org/2009/06/24/wozu-dient-reflection/#respond" style="display:none;">Click here to cancel reply</a></small>
</div>
<form action="https://web.archive.org/web/20090629065657/http://blog.aztec-project.org/wp-comments-post.php" method="post" id="commentform">
<input type="hidden" name="comment_post_ID" value="280" id="comment_post_ID">
<input type="hidden" name="comment_parent" id="comment_parent" value="0">
<p><input type="text" name="author" id="author" value="" size="22" tabindex="1">
<label for="author"><small>Name (benötigt)</small></label></p>
<p><input type="text" name="email" id="email" value="" size="22" tabindex="2">
<label for="email"><small>E-mail (wird nicht veröffentlicht) (benötigt)</small></label></p>
<p><input type="text" name="url" id="url" value="" size="22" tabindex="3">
<label for="url"><small>Webseite</small></label></p>
<p><textarea name="comment" id="comment" cols="100%" rows="10" tabindex="4"></textarea></p>
<p>
<input name="submit" type="submit" id="submit" tabindex="5" value="Kommentar absenden">
<input type="hidden" name="comment_post_ID" value="280">
</p>
<br class="clear">
</form>
</div>
<script type="text/javascript">
<!-- // <![CDATA[
jQuery(document).ready(function() {
if (jQuery('#trackbacklist li').length == 1)
jQuery('#trackbacks, #trackbacklist').hide();
jQuery('li.comment>div>*').filter(':not(.comment-author, .comment-meta)').wrap('<div class="comment"><\/div>');
jQuery('.comment-author').before('<span class="switch-post"><a href="#"><img src="https://web.archive.org/web/20090629065657/http://blog.aztec-project.org/wp-content/themes/mandigo/images/icons/bullet_toggle_minus.png" alt="" class="png" /><\/a><\/span>');
});
// ]]> -->
</script>
</td>
</tr>
</tbody></table>
</div>
<div id="footer" class="png">
<p>
Unterstützt von <a href="https://web.archive.org/web/20090629065657/http://wordpress.org/">WordPress</a>, <a href="https://web.archive.org/web/20090629065657/http://www.onehertz.com/portfolio/wordpress/" target="_blank" title="WordPress themes">Mandigo theme</a> by tom.
<br><a href="https://web.archive.org/web/20090629065657/http://blog.aztec-project.org/feed/"><img src="./reflection_files/rss_s.gif" alt=""> Einträge (RSS)</a>
und <a href="https://web.archive.org/web/20090629065657/http://blog.aztec-project.org/comments/feed/"><img src="./reflection_files/rss_s.gif" alt=""> Kommentare (RSS)</a>.
</p>
</div>
</div>
<script type="text/javascript">
<!-- // <![CDATA[
jQuery(document).ready(function() {
jQuery('#rssicon, #searchsubmit').hover(
function() { this.src = this.src.replace('.gif','_hover.gif'); },
function() { this.src = this.src.replace('_hover.gif','.gif'); }
);
jQuery('.entry img[align=left]' ).addClass('alignleft' );
jQuery('.entry img[align=right]').addClass('alignright');
if (jQuery.browser.msie) {
if (/^[56]/.test(jQuery.browser.version)) {
jQuery.ifixpng('https://web.archive.org/web/20090629065657/http://blog.aztec-project.org/wp-content/themes/mandigo/images/1x1.gif');
jQuery('body, .png').ifixpng();
jQuery('.commentlist.pre27 .avatar').height(36);
}
}
jQuery('p:has(.dropcap)').append('<br class="clear"/>');
togglePost = function(id) {
if (!id) return;
icon = jQuery('#switch-post-'+ id +' img');
icon.attr('src', /minus/.test(icon.attr('src')) ? icon.attr('src').replace('minus', 'plus') : icon.attr('src').replace('plus', 'minus'));
jQuery('#post-'+ id +' .entry').animate({ height: 'toggle', opacity: 'toggle' }, 1000);
}
jQuery('.switch-post img').bind('click', function() {
id = jQuery(this).parents('li').attr('id').replace(/[^\d]+-/, '');
icon = jQuery('#comment-'+ id +' .switch-post img');
src = icon.attr('src');
icon.attr('src', /minus/.test(src) ? src.replace('minus', 'plus') : src.replace('plus', 'minus'));
jQuery('#comment-'+ id +' div.comment').animate({ height: 'toggle', opacity: 'toggle' }, 1000);
return false;
});
toggleSidebars = function() {
icon = jQuery('.switch-sidebars img');
icon.attr('src', /hide/.test(icon.attr('src')) ? icon.attr('src').replace('hide', 'show') : icon.attr('src').replace('show', 'hide'));
jQuery('.sidebars').animate({ width: 'toggle', height: 'toggle', padding: 'toggle', border: 'toggle' }, 1000);
}
jQuery('.widgettitle, .linkcat *:first, .wpg2blockwidget h3').click(function() {
jQuery(this).siblings().animate({ height: 'toggle', opacity: 'toggle' }, 1000);
}).css({cursor: 'n-resize'});
jQuery.each(['tl','tr','bl','br'],function() {
jQuery('#blogname').after('<span class="blogname text-stroke-'+ this +'">'+ jQuery('#blogname a').html() +'<\/span>');
jQuery('#blogdesc').after('<span class="blogdesc text-stroke-'+ this +'">'+ jQuery('#blogdesc' ).html() +'<\/span>');
});
jQuery('#header .page_item:has(ul)').addClass('has_sub, png');
jQuery('#header .page_item').hover(
function() {
ul = jQuery(this).find('>ul');
if (ul.hasClass('sliding')) return;
a = jQuery(this).find('>a');
if (jQuery(this).parent().hasClass('pages')) {
y = a.position().top + a.height();
x = a.position().left + a.width() - ul.width();
}
else {
y = jQuery(this).position().top - 5;
x = jQuery(this).parent().width();
}
jQuery(this)
.find('>ul')
.css({left:x, top:y})
.addClass('sliding')
.slideDown(300, function() { jQuery(this).removeClass('sliding'); });
},
function() {
jQuery(this)
.find('>ul')
.addClass('sliding')
.slideUp(800, function() { jQuery(this).removeClass('sliding').hide(); });
}
);
jQuery('.commentlist li:even').addClass('alt');
});
// ]]> -->
</script>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://web.archive.org/web/20090629065657/https://ssl." : "https://web.archive.org/web/20090629065657/http://www."); document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script><script src="./reflection_files/ga.js.Download" type="text/javascript"></script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-9511047-1"); pageTracker._trackPageview(); } catch(err) {}</script>
<div data-lastpass-root="" style="position: absolute !important; top: 0px !important; left: 0px !important; height: 0px !important; width: 0px !important;"><template shadowrootmode="closed"><div style="position: absolute; height: 100vh; width: 100vw; z-index: 2147483647; border-radius: 4px; top: 0px; left: 0px; max-height: 0px; max-width: 280px; min-width: auto;"><iframe data-lastpass-infield="true" allow="clipboard-write" src="chrome-extension://hdokiejnpimakedhajhdlcegeplioahd/webclient-infield.html" style="border: none; height: 100%; width: 100%;"></iframe></div></template></div></body></html>