Advanced_C#_Skill_Reflection

In this article ,  I 'm going to dig into one of the deep in dark corners

of the C# language.

That is reflection  !!!!





Now  you can think of reflection very similarly how you view your own reflection

when you wake up in the morning and go into the bathroom and

you look at yourself in the mirror you see your reflection

you  think you see things that make up yourself

the characteristics of yourself

your face

your eyes

your ears

your nose

your mouth

arms  ,   shoulders  and so on .

All of those that make you

you will reflection within the C# language does exactly the same thing

it allows your code or your assembly to look within itself and

and take a look at and inspect those characteristics that make itself now it's kind of

interesting when I first began learning about reflection

it's one of those topics where I kind of felt like

this is really cool parlor trick but but it doesn't reallly serve much

of a  purpose for me and that was the case for a while but as I started to play


around with the more I saw that it was very powerful in fact

but  like anything else with great power comes great responsibility

and that if you misuse it

it's definitely going to be to the detriment of the performance

of your software

So let's go ahead and jump into some code.



I'm gonna show you  a little bit about reflection how to use it

what it can do for you and one of the nice little sweet spots that allows

you to use it.

To make your code very dynamic


Here we are back in Visual Studio and new a project for practicing






here we are within our code within our main method

of our class program

The first thing you need when you're talking about reflection

is you actually need  an assembly

to refer act upon or to inspect

so let's do that first

Create some variables

and I'm going to calling it assembly



and now we need to accept that to an assembly so the simplest
way to do that is to use the assembly class  which is found

within  the system .reflection namespace



so I'm going to add that to my file

and then I will call the method GetExecutingAssemnbly


which as you would probably expect gets all the assembly information
about the current executing assembly .




So in our case that's

CS_Reflection_exercise


that I have what's the most logical thing for me to do the first

well that would be to write out some data

to the console so I can show you that I actually have it.


Let's do the Console writeline

and we'll do assembly  dot FullName




do ctrl F5 to run this



you see something rather interesting here

you're gonna see very long string with a little bit more information

thaan you probably had bargained for


and what it is  is the fully qualified name of your assembly which

is made up of four parts

the name of your assembly which in this  case is CS_Reflection_exercise

the current version of that assembly which by default 1.0.0.0

and you can update that within your code if you'd like to

but  for our case we're just going to leave it that

for each version for each build of our application

and thaen the culture  and the public key  token

now these some of these topics are definitely beyond the scope of

this lesson but just understand that these four things put together

can uniquely identify your assembly and thses things are very important

once you get into topics like stronglly-typed assemblies

where somebody's and loaded into the global assembly cash
which is a little bit beyond the scope of this lesson

so let's go ahead and dig back to into our code and

see what other things we can figure out about our assembly

Now we have the name that's pretty cool



But what else can we do

getting the types out of your assembly and what are types at the end of the

day they're really just classes so let's jump back into that

and add more code




















留言

這個網誌中的熱門文章

何謂淨重(Net Weight)、皮重(Tare Weight)與毛重(Gross Weight)

Architecture(架構) 和 Framework(框架) 有何不同?_軟體設計前的事前規劃的藍圖概念

(2021年度)駕訓學科筆試準備題庫歸納分析_法規是非題