Why this article ?

because of this note found on the msdn’s EventInfo page:

“EventInfo is not intended to be used to raise events. An object raises events as dictated by its internal state. ”


Let’s try to raise an event with reflection …

Firstly, let’s search, but not in GetEvent, in GetField :

Type.GetField(String, BindingFlags)

String is the name of the field to get and BindingFlags a bitmask of serarching flags.

Note : To use reflecion, use “System.Reflection” using directive

Let’s try a GetField :

FieldInfo my_event_FieldInfo = this.GetType().GetField("my_event",

BindingFlags.NonPublic

| BindingFlags.Instance);

Yeah ! it’s not null ! we have now the FieldInfo of our event.

What to do now ? in a FieldInfo, we do not have something like “Fire the event”, huh

But what about “GetValue” ? The “Value” of a “FieldInfo” is the field, so the Value of an event’s FieldInfo isn’t the Event ?

FieldInfo.GetValue(object)

let’s try :

object my_event_by_reflection = my_event_FieldInfo.GetValue(this);

Download this code: 2.cs

Is null … ):

Ohhhh but, while not registered, an event is null … so, let’s try adding a handler…

Is not null ! ! Yeah, so we have our event by reflection now.

————————————————————

Note for those who just said “Why giving an object to GetValue ? And why giving ‘this’” :

1) Take a look at the first “this.getType()”

2) Deduce that it ask the Object to give you its Type, an Object knows its Type.

3) Note that a Type can’t know its Objects …

4) Finally to get a Field of your Object, you ask the Type to give it or your Object, giving a reference to your Object, here : ‘this’;

————————————————————

Now, we have an object, which is the event to call, but, how to call it ?

By reflection ? but … where to search ?

TRICK : Put a breakpoint just after the GetValue, when breaked, add a watch on “my_event_by_reflection.GetType()” and browse it… try my_event_by_reflection.GetType().GetFields() … nothing … try my_event_by_reflection.GetType().GetMethods() … browse it … OH ? What is “Invoke” ?

var my_event_invoke = my_event_type.GetMethod("Invoke");

Download this code: 3.cs

Searching how to call the Invoke … by reflection ? Invoking invoke ? Let’s try :

my_event_invoke.Invoke(my_event_by_reflection, new object[] {this, new EventArgs()} );

Download this code: 4.cs

Invoke take the instance of this type, so the object where GetType where called, and in second parameter an array of object to be given as parameter to the method called, we have to give an object and an eventArgs.

It Works ! here the full working source code :

using System.Reflection;

class Cage_en_metal

{

public event EventHandler ecraser;

public Cage_en_metal()

{

ecraser += new EventHandler(Libellule_ecraser);

fireEvent("ecraser", new EventArgs());

}

void Libellule_ecraser(object sender, EventArgs e)

{

Console.WriteLine("Splatch !");

}

void fireEvent(string handler, EventArgs eventArgs)

{

var eventInfo = this.GetType().GetField(handler,

BindingFlags.Instance

| BindingFlags.NonPublic);

if (eventInfo != null)

{

var event_member = eventInfo.GetValue(this);

// Note : If event_member is null, nobody registered to the event, you can't call it.

if (event_member != null)

event_member.GetType().GetMethod("Invoke").Invoke(event_member, new object[] { this, eventArgs });

}

}

}

Add below code to your resources or the style page.
&lt:Style x:Key="GlassBorderStyle" TargetType="Border"&gt:
&lt:Setter Property="BorderThickness" Value="2"/&gt:
&lt:Setter Property="Padding" Value="5"/&gt:
&lt:Setter Property="Background"&gt:
&lt:Setter.Value&gt:
&lt:LinearGradientBrush EndPoint="0.75,1" StartPoint="0.25,0"&gt:
&lt:GradientStop Color="#33FFFFFF" Offset="0"/&gt:
&lt:GradientStop Color="#C0FFFFFF" Offset="0.287"/&gt:
&lt:GradientStop Color="#4011322D" Offset="0.683"/&gt:
&lt:GradientStop Color="#33FFFFFF" Offset="1"/&gt:
&lt:/LinearGradientBrush&gt:
&lt:/Setter.Value&gt:
&lt:/Setter&gt:
&lt:Setter Property="BorderBrush"&gt:
&lt:Setter.Value&gt:
&lt:LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"&gt:
&lt:GradientStop Color="#5811322D" Offset="0"/&gt:
&lt:GradientStop Color="#3EFFFFFF" Offset="0.25"/&gt:
&lt:GradientStop Color="#FFFFFFFF" Offset="0.5"/&gt:
&lt:GradientStop Color="#3EFFFFFF" Offset="0.75"/&gt:
&lt:GradientStop Color="#BFFFFFFF" Offset="1"/&gt:
&lt:/LinearGradientBrush&gt:
&lt:/Setter.Value&gt:
&lt:/Setter&gt:
&lt:Setter Property="Effect"&gt:
&lt:Setter.Value&gt:
&lt:DropShadowEffect BlurRadius="3" ShadowDepth="3" Opacity="0.5"/&gt:
&lt:/Setter.Value&gt:
&lt:/Setter&gt:
&lt:/Style&gt:

now create a button and apply the resource of that Glass Border.
&lt:Border x:Name="GlassBorder" Height="100" Width="250" CornerRadius="10" Style="{StaticResource GlassBorderStyle}"&gt:
&lt:Button Content="Glass Border Style"/&gt:
&lt:/Border&gt:

There are lots of way's to Reduce the Size of Xap file. like.

  1. Just put Multimedia files like images & video at client bin folder where the xap file is.
  2. this ways you will reduce the size of XAP file & take a fast loading.
  3. And also we can reduce size of XAP.. go to sliverlight project in Application ,Right click on it go "properties" see an option called -"Reduce Xap size by using appplication library chaching"
  4. We can move all the dlls (system dlls) out of the XAP files. They will be loaded on demand and will be downloaded from microsoft website/website of your wish.
  5. Split silverlight project into multiple silverlight projects. Load them when you need it. This way you can load what you need and also you save bandwidth.

A basic xap file in silverlight will have an assembly related to specific code for the application, an application manifest file and any additional assemblies need to run the application. At a minimum, two files are needed, the application manifest file and the application assembly. For example:

  • AppManifest.xaml
  • MySiilverlightproject.dll
Once you have created the .xap file, the Silverlight plug-in downloads the file and runs it in a separate work space.

A .xap file is used to contain and transfer the assemblies and resources of a managed code application. This managed code application must be run within the Silverlight 2 browser plug-in.

Every dot net application is complied with the dot net compilers and deployed in dll. (Dynamic Link Library.) same in the Silverlight all deployment of Silverlight is done in the dll. but the deployment of Silverlight are compressed into a xap file. which is the output of the silverlight project.
the XAP file is actualy a ziped files which as all external resource dll into a zipped files

  1. Silverlight uses a particular implementation of a XAML parser, with that parser being part of the Silverlight core install. In some cases, the parsing behavior differs from the parsing behavior in Windows Presentation Foundation (WPF), which also has a particular implementation.
  2. WPF(Windows Presentation Foundation) use for the Desktop Application and SilverLight is use for Web Applications (Rich Internet Applcaions).
  3. Both Have their seperate XAML parser.

  1. Silverlight is a new cross-browser, cross-platform implementation of the .NET Framework for building and delivering the next generation of media experiences and Rich Interactive Applications(RIA) for the web. It runs in all popular browsers, including Microsoft Internet Explorer, Mozilla Firefox, Apple Safari, Opera. The plugin required to run Silverlight is very small in size hence gets installed very quickly.
  2. It is combination of different technolgoies into a single development platform that allows you to select tools and the programming language you want to use. Silverlight integrates seamlessly with your existing Javascript and ASP.NET AJAX code to complement functionality which you have already created.
  3. Silverlight aims to compete with Adobe Flash and the presentation components of Ajax. It also competes with Sun Microsystems' JavaFX, which was launched a few days after Silverlight.
  4. We can also say It is nothing but it has the ability to web applcation is exhibits like the Desktop Application (RIA - Rich Internet Application )which is Cross Browser , Cross Plateform ,Silverlight Aplication can runs on any browser but u neet to installed the Silverlight Plug-Ins which is free to run the applicaion on your browser.