|
|
65.1 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. |
65.2 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#.
|
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.");
|
65.3 Can I generate UML diagrams from C# code?
|
 |
There are several commercial products that support this. Take a look at Metamill and WithClass
|
65.4 Does .Net support "deprecation" 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.
|
65.5 Can .NET applications run on non Microsoft Platforms?
|
 |
65.6 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.
|
65.7 Can I create file associations with .NET?
|
 |
65.8 Is there any way to Shutdown/Restart Windows using .NET?
|
 |
65.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:
|
// 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);
|
mi.Invoke(someInsance, new object[]{});
|
' 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)
|
65.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. |
65.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:
|
65.12 How do I pre-JIT an assembly to native code during installation?
|
 |
65.13 How do I get the install directory for the version of the runtime that is loaded in the current process?
|
 |
For example, the "C:\WINNT\Microsoft.NET\Framework\v1.0.3705" dir for the 1.0 framework version.
You can do so using PInvoke to call this native API method:
|
[DllImport("mscoree.dll")]
|
internal static extern void GetCORSystemDirectory([MarshalAs(UnmanagedType.LPTStr)]System.Text.StringBuilder Buffer,
|
int BufferLength, ref int Length);
|
// Gets the path to the Framework directory.
|
System.Text.StringBuilder sb = new System.Text.StringBuilder(1024);
|
// returned value in size can be ignored
|
GetCORSystemDirectory(sb, sb.Capacity, ref size);
|
Private Declare Function GetCORSystemDirectory Lib "mscoree.dll" ( ByVal Buffer As System.Text.StringBuilder, ByVal BufferLength As Integer, ByRef Length As Integer) As Integer
|
' Gets the path to the Framework directory.
|
Dim Path As New System.Text.StringBuilder(1024)
|
' returned value in Size can be ignored
|
GetCORSystemDirectory(Path, Path.Capacity, Size)
|
65.14 How to retrieve the assembly version, name, etc. contained in the "AssemblyInfo.cs", from inside the running app?
|
 |
Here is a way to do this:
1) string strVersion = Application.ProductVersion;
|
2) Assembly assembly = Assembly.GetCallingAssembly();
|
string strVersion = assembly.GetName().Version.ToString();
|
The Application class offers also access to additional information like ProductName and so on.
(Information provided by Holger Persch in our forums) |
65.15 Is there a way to access private and protected members of a class instance?
|
 |
.NET Reflection can be used to get hold of non-Public members of a class instance. The System.Type class provides various methods that allow you to access the fields/properties/methods for all defined types. Depending on the context, you can use either the typeof(class) operator or the Object.GetType() method to get hold of the System.Type object representing the particular type and then use that type object to access the required member information.
The following code shows how to use the Type.GetField() and FieldInfo.GetValue() methods to determine the value of the 'pasteIndex' private field implemented by the Diagram.Controller class.
|
// Use Reflection to access the this.ActiveDiagram.Controller object's pasteIndex private member
|
Type typecontroller = typeof(Syncfusion.Windows.Forms.Diagram.Controller);
|
System.Reflection.FieldInfo finfo = typecontroller.GetField("pasteIndex", BindingFlags.Instance|BindingFlags.NonPublic|BindingFlags.GetField);
|
npasteindex = (int)finfo.GetValue(this.ActiveDiagram.Controller);
|
' Use Reflection to access the Me.ActiveDiagram.Controller object's pasteIndex private member
|
Dim typecontroller As Type = GetType(Syncfusion.Windows.Forms.Diagram.Controller)
|
Dim finfo As FieldInfo = typecontroller.GetType().GetField("pasteIndex", BindingFlags.Instance Or BindingFlags.NonPublic Or BindingFlags.GetField)
|
Dim npasteindex As Integer = 0
|
If Not finfo Is Nothing Then
|
npasteindex = finfo.GetValue(Me.ActiveDiagram.Controller)
|
65.16 In VB.NET how do I access the runtime type object of a specified type from it's type name?
|
 |
Visual Basic has a GetType operator that can be used to obtain the System.Type object for the specified type name. This is the equivalent of the typeof keyword in C#.
|
' Returns the System.RuntimeType object representing the integer type.
|
Dim inttype As Type = GetType(Integer)
|
' Returns the System.RuntimeType object representing the Project1.Type1 class.
|
Dim mytype As Type = GetType(Project1.Type1)
|
65.17 How do I print a document using .NET?
|
 |
This can be done using the Process class and specifying the verb as "Print". This is the equivalent of right clicking and selecting print in the windows shell. Here is the code snippet that prints documents like MS Excel, MS Word, pdf etc..
|
//Print an Excel document
|
Process pr = new Process();
|
pr.StartInfo.Verb = "Print";
|
pr.StartInfo.FileName = "Sample.xls";
|
Dim pr As Process = New Process()
|
pr.StartInfo.Verb = "Print"
|
pr.StartInfo.FileName = "Sample.xls"
|
|
|
|
|