Tag Archives: tutorial

Using Reflection

Reflection gives you the ability to inspect the code of a .NET application and execute that code dynamically. Various tools, such as VS 2012 Web Services and unit testing use reflection to perform actions based on the code you’ve written. When building a Web Service, VS 2012 uses reflection to inspect the attributes applied to classes and then read the method names, parameters, and return types of those methods to build an XML contract for clients to consume.The Unit Test runner uses reflection to find classes that contain tests and execute the methods decorated with test attributes.

Getting Type and TypeInfo

The first step in using reflection is to obtain a Type type or a TypeInfo type. A type is a class, struct, interface, or delegate that you write in order to inspect and learn more about the type. TheType andTypeInfo types let you furtherdelve into the internals of a type, using reflection, to learn about the membersof a type. The Type type has been part of .NET since v1.0. Microsoft introduced the TypInfo type in .NET v4.5, which also supports Windows 8
applications. The difference between Type and TypeInfo in .NET 4.5 is that Type provides reference support and TypeInfo is intended for execution.

If you already have an instance of an object, you can get its Type through the System.Object’s GetType method, like this:

In this example, the code has an instance of Program, prog, making it possible to call GetType to obtain the Type object. TheType object has manymethods for inspecting the type and it contains assembly information. This example prints the Name property, which is Program

Another way to obtain a Type is via the typeof operator. This is a C# operatorfrom basic C# syntax, but now you understand its true purpose and how to use it. Here’s an example:

You give the name of the type to the typeof operator and it returns a Type instance for that type. This example uses the AssemblyQualifiedName, whichgives the fully qualified name of the assembly, as follows:

With a Type, you can get a TypeInfo. Here’s an example:

The example above shows that you must first have a Type, which is testType in this case. With that Type , you can call GetTypeInfo to obtain a TypeInfo .


JoeMayoThis post is an excerpt from the online courseware for our C# 2012: Attributes, Reflections and Dynamic
course written by expert Joe Mayo.

Joe Mayo is an author, independent consultant, and instructor specializing in Microsoft .NET and Windows 8 technology. He has several years of software development experience and has worked with .NET since July 2000.

Using Code-Focused Rapid Application Development (RAD)

There are many visual designers in Visual Studio that allow you to drag-and-drop
controls onto the screen, in what is often referred to as Rapid Application
Development (RAD). However, most applications of any significance require
code that defines the logic of that application. Visual Studio recognizes this by
supporting Code-Focused RAD—tools that help you write code faster. This
section shows a few of the ways you can navigate code, and how to add
snippets and refactor code.

Navigating Code

Visual Studio has several features supporting code navigation, including Call

Hierarchy, Navigate To, Highlighting References, and Live Semantic Errors.
The last two are not really navigation features, but they do help you understand
code better.

Call Hierarchy

Call Hierarchy allows you to view callers and callees to a type member in your
code. You can open the Call Hierarchy window (see Figure 8) by selecting the
type member name you’re interested in and pressing Ctrl+K+T.

RAD

Call Hierarchy lets you view callers and callees to a type member.

The Call Hierarchy window also lets you navigate through code. To navigate
to a specific location, double-click on that location in the Call Hierarchy
window. You can open the branches of the tree by clicking an arrow to view
either Calls To or Calls From the location of the arrow.

Navigate To

Navigate To lets you quickly move to anywhere in your code by typing a
search term. Type Ctrl+, (comma) to display the Navigate To window
shown in the Figure below.

RAD1

The Figure above shows how Navigate To lets you search and move to code quickly.

After the Navigate To window appears, you can type a Search term and all the
matching members will appear in the Results window. You can double-click
any item, or select an item and click the OK button, and Visual Studio will
take you to the right location.

Highlighting References

The Highlighting References feature allows you to select an identifier and
Visual Studio highlights all the references to that identifier. Figure below shows
how this works.

RAD2

The Figure above. Highlight References shows the selected identifier and references.

In the Figure above, we selected the method identifier, Level2. Visual Studio
highlighted the reference to that identifier inside the Main method.

Live Semantic Errors

Live semantic errors show you problems with your code as you type. You can
see compiler errors and warnings without building your code. Figure 11 shows
what a Live Semantic Error looks like.

RAD3

The Figure above. Live Semantic Errors show you problems as you type.

The example in the Figure above is missing an end quote, which is a compiler error.
The Live Semantic Error appears as a squiggly line under the location of the
error. For warnings, you receive a green squiggly line.


JoeMayoThis post is an excerpt from the online courseware for our C# 2012: Visual Studio 2012 IDE
course written by expert Joe Mayo.

Joe Mayo is an author, independent consultant, and instructor specializing in Microsoft .NET and Windows 8 technology. He has several years of software development experience and has worked with .NET since July 2000.

Adding Toast Launch Parameters

When users click or tap your toast notification, Windows loads your
application, and users expect your application to have information about the
context of the notification and display the appropriate information. For
example, if users see a notification that a new email has arrived, they expect
that clicking the notification will display the new email message.

To make this happen, you can and should specify the Launch property of the
toast content, when you create the toast notification. Doing this allows
Windows to pass the value of the Launch property to the event argument for
the application’s OnLaunched event, in the argument’s Arguments property.

What happens next? That’s up to you. Retrieve the Arguments property of the
OnLaunched event’s parameter, and take specific action based on the value.
You can simply pass the value along, or act on it. The sample that follows adds
a little extra code so that if the Arguments property of the parameter to the
OnLaunched event isn’t null, it passes the value along to MainPage.xaml as it
loads it. Code in the page’s OnNavigatedTo event displays the parameter it
was passed.

NOTE

If the toast notification doesn’t have its Launch property set, and if
the app is already running, Windows won’t fire the OnLaunched
event. The event always occurs if the app isn’t currently running.

Try It Out!

Follow these steps to demonstrate passing parameters from the toast to a
running application.

1. In Visual Studio, verify that you have the same sample project loaded as in
the previous demonstration, XMLToastDemo. (If you skipped that
exercise, you can start with the project in the XMLToastDemo_ex07
folder.)

2. In the Solution Explorer window, double-click MainPage.xaml.

3. In the StackPanel element, immediately before the closing element, add
the following markup:

 

Style=”{StaticResource ItemTextStyle}”/>

4. In the Solution Explorer window, right-click App.xaml and select View
Code from the context menu.

5. In the OnLaunched override, note the LaunchActivatedEventArgs
parameter—this parameter contains an Arguments property that contains
the Launch property specified in the toast notification.

6. In the OnLaunched override, immediately above the code that calls
Windows.CurrentActivate, add the following code:

7. In MainPage.xaml.cs, in the OnNavigatedTo override, add the following
code:

8. In the CreateScheduledNotification method, immediately below the code
that sets the Alt property of the image, add this code:

9. Save and run the sample.

10. Add a scheduled notification, and then return to Visual Studio and select
DEBUG|Stop Debugging (so the app is no longer running).

11. Wait for the notification, and then click it. This should load the
application, and display the parameter value (Sample parameter) in the
text block at the bottom of the controls.

12. Press ALT+F4 to shut down the application.

NOTE

This technique works if the application is still running, as well. It’s just slightly more interesting if it’s not running when the notification appears.

ldn-expertkgetz

This post is an excerpt from the online courseware for ourWindows 8 Using XAML: Views, Resources, and Toastscourse written by expert Ken Getz.

Ken Getz is a Visual Studio expert with over 25 years of experience as a successful developer and consultant. He is a nationally recognized author and speaker, as well as a featured instructor for LearnNowOnline.

Scheduling Toast Notifications

You can schedule a toast to appear any time in the future—you supply a time
at which you want the toast notification to appear, and it shows up on demand.
Your application doesn’t even have to be running! In order to better manage
your toasts, however, you should set the Id property of the toast to a string of
up to 16 characters. Having an ID allows you to refer to the toast (and possibly
cancel it).

Note that all the previous examples have used a ToastNotifier instance, but
haven’t stored a reference to it for later use. The ToastNotifier class provides
several useful methods, and if you plan to work with toast notifications after
they’re created, you will need to create a specific reference to a ToastNotifier
instance to work with. Specifically, the ToastNotifier class provides these
members:

  • AddToSchedule, RemoveFromSchedule
  • Show (which you previously called), Hide
  • GetScheduledToastNotifications

To create a scheduled notification, start by creating an instance of the
ScheduledToastNotification class. To its constructor, pass the XML content of
a ToastContent instance—you can use the GetXml method to retrieve the
XML content. Next, create the ToastNotifier instance, and call its
AddToSchedule method, supplying the ScheduledToastNotification instance as
a parameter. In addition, supply the time at which you want to display the
notification.

NOTE

You cannot schedule more than 4096 notifications. Also note that
you cannot schedule a notification in the past (in other words, you
can’t break the inviolable laws of the space-time continuum).

Creating a Recurring Notification
To create a recurring notification, follow the exact same steps as for a
scheduled notification, except that you must also specify the snooze interval
(that is, how long to wait before reattempting the notification), which can be a
value between 60 seconds and 60 minutes. Also, specify the maximum occurrences
of the notification (a value between 1 and 5).

TIP: Specify an Id property when creating scheduled and/or recurring toast
notifications. Doing so will make it easier to identify notifications when
reviewing or cancelling them.

ldn-expertkgetz

This post is an excerpt from the online courseware for ourWindows 8 Using XAML: Views, Resources, and Toastscourse written by expert Ken Getz.

Ken Getz is a Visual Studio expert with over 25 years of experience as a successful developer and consultant. He is a nationally recognized author and speaker, as well as a featured instructor for LearnNowOnline.