главная продукты downloads форум тех. поддержка

 

Главная страница

Framework Tips General

FAQ Home

1. How can I catch an exception on a program wide basis?
2. Can I generate UML diagrams from C# code?
3. Is it possible to turn off strong name validation for an assembly?
4. Does .Net support "deprication" as in Java?
5. Is there any way to Shutdown/Restart Windows using .NET?
6. Can .NET applications run on non Microsoft Platforms?
7. Can I play audio and video files using .NET?
8. Can I create file associations with .NET?
9. How can I Invoke a private interface implementations of an instance using reflection?
10. How can I adjust the version number for multiple projects without changing every AssemblyInfo.cs file constantly?
11. How do I launch IE or any other process from code?
 



1 How can I catch an exception on a program wide basis?

You can handle the Application.ThreadException event. See the FrameWork class library for a more detailed sample in both VB abd C#.

     [STAThread]
     public static void Main()
     {
          Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(UnhandledExceptionCatcher);
          Application.Run(new Form1());
     }

     private static void UnhandledExceptionCatcher(object sender, System.Threading.ThreadExceptionEventArgs e)
     {
          Console.WriteLine("caught an unhandled exception.");
     }
 



2 Can I generate UML diagrams from C# code?

There are several commercial products that support this. Take a look at Metamill and WithClass
 



3 Is it possible to turn off strong name validation for an assembly?

You can turn off strong name validation for an assembly by using the sn.exe utility that ships with the framework.
The command line is:

sn -Vr assemblyname

This is helpful if you want to add an assembly to the GAC that is delay signed.
 



4 Does .Net support "deprication" as in Java?

Yes, use the Obsolete attribute on the property, method, etc. You can also provide some deprecation message when you specify this attribute.
 



5 Is there any way to Shutdown/Restart Windows using .NET?

Take a look at classes available here: http://www.mentalis.org/soft/class.qpx?id=7.
 



6 Can .NET applications run on non Microsoft Platforms?

Check out the Mono Project.
http://www.go-mono.com/
 



7 Can I play audio and video files using .NET?

You cannot directly do this using .NET. However, there are some wrapper classes available at http://www.mentalis.org/soft/class.qpx?id=1 that allow you to do just this. You can download the latest version of these classes directly from the site.


We have packaged these classes as a library and added a simple Winforms sample and made these available here.
 

This library basically uses the Media Control Interface (MCI). More details are available here and in several other places.
 



8 Can I create file associations with .NET?

Please take a look at classes available here: http://www.mentalis.org/soft/class.qpx?id=5.
 



9 How can I Invoke a private interface implementations of an instance using reflection?

Make sure to preface the method/property name with the fully qualified interface name that it belongs to, like this:

     // In C#
     // Where someType implements ISomeInterface's SomeMethod as a private implementation.
     Type someType = someInstance.GetType();
     MethodInfo mi = someType.GetMethod("System.Windows.Forms.ISomeInterface.SomeMethod",
               BindingFlags.NonPublic | BindingFlags.Instance);
     if(mi != null)
     {
          mi.Invoke(someInsance, new object[]{});
     }

     ' In VB.Net
     ' Where someType implements ISomeInterface's SomeMethod as a private implementation.
     Dim someType As Type = someInstance.GetType()
     MethodInfo mi = someType.GetMethod("System.Windows.Forms.ISomeInterface.SomeMethod",
               BindingFlags.NonPublic | BindingFlags.Instance)
     If Not mi Is Nothing Then
          ' Assuming no arguments for SomeMethod.
          mi.Invoke(someInsance, Nothing)
     End If
 



10 How can I adjust the version number for multiple projects without changing every AssemblyInfo.cs file constantly?

The version information needs to be set with the "AssemblyVersion" attribute for each project. However, this attribute does not need to be specified in the project's AssemblyInfo file. You can create a separate file with the "AssemblyVersion" attribute that is shared across projects that you wish to keep in sync. This file can either be shared via VSS, or by including the file as a link in the projects.
 



11 How do I launch IE or any other process from code?

You can do so using the System.Diagnostics.Process.Start method as follows:

// Just specifying the document name will open the appropriate app based on system settings.
Process.Start("Http://msdn.microsoft.com")
// Open a word document in Word.
Process.Start("AWordDocument.doc")
// Or open the app directly:
Process.Start()
 

 

e-mail:

 

bousoft@mail.ru