Simple Type Percent for XSD Files

To use the simply type percent (xs:decimal), see the snippet below.

<xs:simpleType name="percent">
  <xs:annotation>
    <xs:documentation>The percent type specifies a value from 0 to 100.</xs:documentation>
  </xs:annotation>
  <xs:restriction base="xs:decimal">
    <xs:minInclusive value="0"/>
    <xs:maxInclusive value="100"/>
  </xs:restriction>
</xs:simpleType>

What is the difference between typeof(), GetType() and IS in C# and VB.NET

What is the difference between typeof(), GetType() and is?

  • typeof() takes a type name specified at compile Time.
  • GetType() gets the Type of an Object at runtime
  • is returns true if an instance is in the inheritance tree. Since the is Keyword is casting the object, you should use it with care.
  • here is an example.

    class Employee { } 
    class Developer : Employee { }
    
    void PrintTypes(Employee e) { 
        print(e.GetType() == typeof(Employee)) // false 
        print(e is Employee)                   // true 
        print(e.GetType() == typeof(Developer))    // true
    }
    

    How to get all types that implement a specific Interface in C# and VB.NET

    To retrieve a IEnuemerable of all types in the current or another Assembly that implement a specific Interface / Abstract class and so on, you can use the snippets below.

    Methods

    C# Version

    VB.NET Version

    Compatibility: working .NET 2.0 working .NET 3.0 not tested .NET 3.5 not working .NET 4.0 not working .NET 4.5

    Do you have an alternate or better method for this task?
    If you have any questions or suggestions feel free to rate this snippet, post a comment or Contact Us via Email.

    Related links:

    Generic Type Conversion snippet for C# and VB.NET

    Here you can find a generic Type conversion snippet for C# and VB.NET.

    Sample C#

    public static T GenericConvert<T, X>(X value) where X : IConvertible
    {
    	return (T)Convert.ChangeType(value, typeof(T));
    }
    

    Sample VB.NET

    Public Shared Function GenericConvert(Of T, X As IConvertible)(value As X) As T
    	Return DirectCast(Convert.ChangeType(value, GetType(T)), T)
    End Function
    

    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 type of a generic parameter in C# and VB.NET

    To get the type of a generic parameter in C# and VB.NET you can use the following snippet.

    Sample C#

    Type typeParam = typeof(T);
    

    Sample VB.NET

    Dim typeParam As Type = GetType(T)
    

    How to style a link by filetype in CSS

    To style a link by filetype in CSS you can use the following snippet.

    Sample CSS

    a[href$=".exe"] { background: url(icons/exe.png) left center no-repeat; padding-left: 50px; }
    a[href$=".rar"] { background: url(icons/rar.png) left center no-repeat; padding-left: 50px; }
    a[href$=".doc"] { background: url(icons/doc.png) left center no-repeat; padding-left: 50px; }
    a[href$=".pdf"] { background: url(icons/pdf.png) left center no-repeat; padding-left: 50px; }
    a[href$=".zip"] { background: url(icons/zip.png) left center no-repeat; padding-left: 50px; }
    a[href$=".html"] { background: url(icons/link.png) left center no-repeat; padding-left: 50px; }
    

    IsOfType extension method for C# and VB.NET

    The following snippet will allow you to use the IsOfType extension method, simple but usefull!

    Extension method C#

    public static bool IsOfType<T>(this object input)
    {
    	return input.IsOfType(typeof (T));
    }
    
    public static bool IsOfType(this object input, Type type)
    {
    	return (input.GetType() == type);
    }
    

    Extension method VB.NET

    <System.Runtime.CompilerServices.Extension> _
    Public Shared Function IsOfType(Of T)(obj As Object) As Boolean
    	Return input.IsOfType(GetType(T))
    End Function
    
    <System.Runtime.CompilerServices.Extension> _
    Public Shared Function IsOfType(input As Object, type As Type) As Boolean
    	Return (input.GetType() = type)
    End Function