How to read a embedded resource to String in C# and VB.NET

To read a embedded resource to String in C# and VB.NET you can use the snippet below.
Keep in mind that this snippet is using Encoding.Default which might not always work in all situations correctly.

Sample C#

Sample VB.NET (autogenerated)

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>
    

    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
  • 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
    

    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
    

    How to get the BuildDate of a Assembly in C# and VB.NET

    Sample C#

    public DateTime GetBuildDate(Assembly assembly)
    {
    	var location = assembly.Location;
    	const int headerOffset = 60;
    	const int linkerTimestampOffset = 8;
    	var buffer = new byte[2048];
    	Stream stream = null;
    
    	try
    	{
    		stream = new FileStream(location, FileMode.Open, FileAccess.Read);
    		stream.Read(buffer, 0, 2048);
    	}
    	finally
    	{
    		if (stream != null)
    		{
    			stream.Close();
    		}
    	}
    
    	var i = BitConverter.ToInt32(buffer, headerOffset);
    	var secondsSince1970 = BitConverter.ToInt32(buffer, i + linkerTimestampOffset);
    	var dt = new DateTime(1970, 1, 1, 0, 0, 0);
    	dt = dt.AddSeconds(secondsSince1970);
    	dt = dt.AddHours(TimeZone.CurrentTimeZone.GetUtcOffset(dt).Hours);
    	return dt;
    }
    

    Sample VB.NET

    Public Function GetBuildDate(assembly as Assembly) As DateTime
    	Dim location = assembly.Location
    	Const  headerOffset As Integer = 60
    	Const  linkerTimestampOffset As Integer = 8
    	Dim buffer = New Byte(2047) {}
    	Dim stream As Stream = Nothing
    
    	Try
    		stream = New FileStream(location, FileMode.Open, FileAccess.Read)
    		stream.Read(buffer, 0, 2048)
    	Finally
    		If stream IsNot Nothing Then
    			stream.Close()
    		End If
    	End Try
    
    	Dim i = BitConverter.ToInt32(buffer, headerOffset)
    	Dim secondsSince1970 = BitConverter.ToInt32(buffer, i + linkerTimestampOffset)
    	Dim dt = New DateTime(1970, 1, 1, 0, 0, 0)
    	dt = dt.AddSeconds(secondsSince1970)
    	dt = dt.AddHours(TimeZone.CurrentTimeZone.GetUtcOffset(dt).Hours)
    	Return dt
    End Function
    

    How to use the assembly InternalsVisibleTo attribute

    If your code contains classes, interfaces or structs that have the internal (c#) access qualifier or the Friend (VB.NET) access qualifier, you cannot access them from another assembly (e.g. unit testing). Types have this qualifier for one of two reasons

    – You have explicitly marked them as internal.
    – By default, top-level classes, interfaces, or structs that have no explicit access qualifier are treated as internal.

    But what if you need to make them visible just for a specific assembly? Thats where the assembly InternalsVisibleTo attribute comes into play.
    If your Assembly which has the Internal access qualifiers has no strong name you simply have to put the following line of code to its AssemblyInfo-File.

    Sample C#

    [assembly:InternalsVisibleTo("OtherAssemblyName")]
    

    Sample VB.NET

    <Assembly:InternalsVisibleTo("OtherAssemblyName")>
    

    If your Assembly which has the Internal access qualifiers has strong name you simply have to put the following line of code to its AssemblyInfo-File.
    The PublicKey depends on your Assembly, so you need to put the right PublicKey in it, check the links on the bottom of this Posts on how to get the PublicKey.

    Sample C#

    [assembly:InternalsVisibleTo("OtherAssemblyName,PublicKey=00240000048000009400000006020000002400005253413100040000010001002B4D3670141EEBB497143AD5400105B520B65C7CC91076C86BDEBDFB9BBB527D4B98F51F70D12B92008DB4D0715C26E7C4857CD1D44EC12AD17CED8E7DC94CD4F28E4962BB88EF6224F4BC6C1A7F35238383B70E56323B48B1C57F4E0510C0E9C5625A0A485AB22A03C7B199CB529FB234D279F883633A18509AC231B643E1E5")]
    

    Sample VB.NET

    <Assembly:InternalsVisibleTo("OtherAssemblyName,PublicKey=00240000048000009400000006020000002400005253413100040000010001002B4D3670141EEBB497143AD5400105B520B65C7CC91076C86BDEBDFB9BBB527D4B98F51F70D12B92008DB4D0715C26E7C4857CD1D44EC12AD17CED8E7DC94CD4F28E4962BB88EF6224F4BC6C1A7F35238383B70E56323B48B1C57F4E0510C0E9C5625A0A485AB22A03C7B199CB529FB234D279F883633A18509AC231B643E1E5")>
    

    More informations: Setting the InternalsVisibleTo Attribute, Getting Public Key Token of Assembly Within Visual Studio, AssemblyHelperTool

    How to read a embedded resource image in C# and VB.NET

    To read a embedded resource image in C# and VB.NET you can use the following snippet.

    Sample C#

    public static Image ReadEmbeddedRessourceImage(Assembly assembly, string searchPattern)
    {
    	var resourceName = assembly.GetManifestResourceNames().FirstOrDefault(x => x.Contains(searchPattern));
    	using (var stream = assembly.GetManifestResourceStream(resourceName))
    	{
    		if (stream != null) return Image.FromStream(stream);
    	}
    	return null;
    }
    

    Sample VB.NET

    Public Shared Function ReadEmbeddedRessourceImage(assembly As Assembly, searchPattern As String) As Image
    	Dim resourceName = assembly.GetManifestResourceNames().FirstOrDefault(Function(x) x.Contains(searchPattern))
    	Using stream = assembly.GetManifestResourceStream(resourceName)
    		If stream IsNot Nothing Then
    			Return Image.FromStream(stream)
    		End If
    	End Using
    	Return Nothing
    End Function
    

    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
    

    How to get the Calling Assembly in C# or VB.NET

    To get the Calling Assembly in C# or VB.NET you can use Reflection.

    Sample C#

    using System.Reflection;
    
    public static string GetCallingAssemblyName()
    {
    var callingAssembly = Assembly.GetCallingAssembly();
    return callingAssembly.FullName;
    }
    

    Sample VB.NET

    Imports System.Reflection
    
    Public Shared Function GetCallingAssemblyName() As String
        Dim callingAssembly = Assembly.GetCallingAssembly()
        Return callingAssembly.FullName
    End Function
    

    for more informations see the MSDN: Reflection (C# and Visual Basic), Assembly.GetCallingAssembly Method