How to load project references from a sub directory in C# and VB.NET

To load project references from a sub directory in C# and VB.NET you simply need to add the element to your app.config file.

  • assemblyBinding: Contains information about assembly version redirection and the locations of assemblies.
  • configuration: The root element in every configuration file used by the common language runtime and .NET Framework applications.
  • runtime: Contains information about assembly binding and garbage collection.
  • Sample XML

    <configuration>
       <runtime>
          <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
             <probing privatePath="bin;bin2\subbin;bin3"/>
          </assemblyBinding>
       </runtime>
    </configuration>
    

    Please rate this snippet

    How to get return Value from method invocation using reflection in C# and VB.NET

    To get return Value from method invocation using reflection in C# and VB.NET you can use the snippet below.

    Sample C#

    var returnValue = (Int32)methodInfo.Invoke(null,null); //just cast the returned object of Invoke to the needed Type. Theres no difference if it is a static method/class or not
    

    Sample VB.NET

    Dim returnValue = DirectCast(methodInfo.Invoke(Nothing, Nothing), Int32) 'just cast the returned object of Invoke to the needed Type. Theres no difference if it is a static method/class or not
    

    For more informations on how to invoke methods from other assemblies using reflection see:

  • How to call a static method in different assembly using Reflection in C# and VB.NET
  • How to call a non-static method in different assembly using Reflection in C# and VB.NET
  • Please rate this snippet

    How to call a non-static method in different assembly using Reflection in C# and VB.NET

    To call a static method in different assembly using Reflection in C# and VB.NET you can use the snippet below.

    Sample C#

    Assembly assembly = Assembly.LoadFile(@"Assembly_Location");   
    var type = assembly.Types().FirstOrDefault(x => x.FullName.Contains("MyClassName"));
    var constructor = type.GetConstructor(Type.EmptyTypes);
    
    var classObject = constructor.Invoke(new object[] { }); //call parameterless constructor
    var classObject = constructor.Invoke(new object[] { param1 }); //call non-parameterless constructor
    
    MethodInfo methodInfo = type.GetMethod("MyMethodName");
    methodInfo.Invoke(classObject, new object[] { }); //pass classobject, parameterless method
    methodInfo.Invoke(classObject, new object[] { param1 }); //pass classobject, parameterized method
    

    Sample VB.NET

    Dim assembly__1 As Assembly = Assembly.LoadFile("Assembly_Location")
    Dim type__2 = assembly__1.Types().FirstOrDefault(Function(x) x.FullName.Contains("MyClassName"))
    Dim constructor = type__2.GetConstructor(Type.EmptyTypes)
    
    Dim classObject = constructor.Invoke(New Object() {})'call parameterless constructor
    Dim classObject = constructor.Invoke(New Object() {param1})'call non-parameterless constructor
    
    Dim methodInfo As MethodInfo = type__2.GetMethod("MyMethodName")
    methodInfo.Invoke(classObject, New Object() {})'pass classobject, parameterless method
    methodInfo.Invoke(classObject, New Object() {param1})'pass classobject, parameterized method
    

    Please rate this snippet

    How to call a static method in different assembly using Reflection in C# and VB.NET

    To call a static method in different assembly using Reflection in C# and VB.NET you can use the snippet below.

    Sample C#

    Assembly assembly = Assembly.LoadFile(@"Assembly_Location"); 
    var t = assembly.Types().FirstOrDefault(x=>x.FullName.Contains("MyClassName"));
    var method = t.GetMethod("MyMethodName", BindingFlags.Public | BindingFlags.Static);
    method.Invoke(null, null); //assuming that the method does not have parameters
    method.Invoke(null, new object[] { param1}); //assuming that the method has 1 parameter
    

    Sample VB.NET

    Dim assembly__1 As Assembly = Assembly.LoadFile("Assembly_Location")
    Dim t = assembly.Types().FirstOrDefault(Function(x) x.FullName.Contains("MyClassName"))
    Dim method = t.GetMethod("MyMethodName", BindingFlags.[Public] Or BindingFlags.[Static])
    method.Invoke(Nothing, Nothing) 'assuming that the method does not have parameters
    method.Invoke(Nothing, New Object() {param1}) 'assuming that the method has 1 parameter
    

    Please rate this snippet

    Assembly version info extension methods for C# and VB.NET

    This snippet will give you Assembly version info extension methods for C# and VB.NET.

    Sample C#

    public static string GetVersion(this Assembly assembly)
    {
    	if (assembly == null)
    	{
    		throw new ArgumentNullException();
    	}
    	var assemblyName = assembly.GetName();
    	var version = assemblyName.Version;
    	return version.ToString();
    }
    
    public static string GetMajor(this Assembly assembly)
    {
    	if (assembly == null)
    	{
    		throw new ArgumentNullException();
    	}
    	var assemblyName = assembly.GetName();
    	var version = assemblyName.Version;
    	return version.Major.ToString();
    }
    
    public static string GetMinor(this Assembly assembly)
    {
    	if (assembly == null)
    	{
    		throw new ArgumentNullException();
    	}
    	var assemblyName = assembly.GetName();
    	var version = assemblyName.Version;
    	return version.Minor.ToString();
    }
    
    public static string GetBuild(this Assembly assembly)
    {
    	if (assembly == null)
    	{
    		throw new ArgumentNullException();
    	}
    	var assemblyName = assembly.GetName();
    	var version = assemblyName.Version;
    	return version.Build.ToString();
    }
    
    public static string GetRevision(this Assembly assembly)
    {
    	if (assembly == null)
    	{
    		throw new ArgumentNullException();
    	}
    	var assemblyName = assembly.GetName();
    	var version = assemblyName.Version;
    	return version.Revision.ToString();
    }
    

    Sample VB.NET

    <System.Runtime.CompilerServices.Extension> _
    Public Shared Function GetVersion(assembly As Assembly) As String
    	If assembly Is Nothing Then
    		Throw New ArgumentNullException()
    	End If
    	Dim assemblyName = assembly.GetName()
    	Dim version = assemblyName.Version
    	Return version.ToString()
    End Function
    
    <System.Runtime.CompilerServices.Extension> _
    Public Shared Function GetMajor(assembly As Assembly) As String
    	If assembly Is Nothing Then
    		Throw New ArgumentNullException()
    	End If
    	Dim assemblyName = assembly.GetName()
    	Dim version = assemblyName.Version
    	Return version.Major.ToString()
    End Function
    
    <System.Runtime.CompilerServices.Extension> _
    Public Shared Function GetMinor(assembly As Assembly) As String
    	If assembly Is Nothing Then
    		Throw New ArgumentNullException()
    	End If
    	Dim assemblyName = assembly.GetName()
    	Dim version = assemblyName.Version
    	Return version.Minor.ToString()
    End Function
    
    <System.Runtime.CompilerServices.Extension> _
    Public Shared Function GetBuild(assembly As Assembly) As String
    	If assembly Is Nothing Then
    		Throw New ArgumentNullException()
    	End If
    	Dim assemblyName = assembly.GetName()
    	Dim version = assemblyName.Version
    	Return version.Build.ToString()
    End Function
    
    <System.Runtime.CompilerServices.Extension> _
    Public Shared Function GetRevision(assembly As Assembly) As String
    	If assembly Is Nothing Then
    		Throw New ArgumentNullException()
    	End If
    	Dim assemblyName = assembly.GetName()
    	Dim version = assemblyName.Version
    	Return version.Revision.ToString()
    End Function
    

    Please rate this snippet