How to select an element with its name attribute in jQuery

To select an element with its name attribute in jQuery you can use the snippet below.

Samples

Javascript Sample

if you remove the div in div[name=”de.Fesslersoft.Testname”] you will get all elements with the specific name.

You can also have a look at this JSFiddle.

If you have any questions or suggestions feel free to rate this snippet, post a comment or Contact Us via Email.

Related links:

Please rate this snippet

How to get all Description Attributes of an Enum using C# or VB.NET

To get all Description Attributes of an Enum using C# or VB.NET you can use the snippet below.

Sample C# – Class

public class DescriptionAttributes<T>
    {
        protected List<DescriptionAttribute> Attributes = new List<DescriptionAttribute>();
        public List<string> Descriptions { get; set; }

        public DescriptionAttributes()
        {
            RetrieveAttributes();
            Descriptions = Attributes.Select(x => x.Description).ToList();
        }

        private void RetrieveAttributes()
        {
            foreach (var attribute in typeof(T).GetMembers().SelectMany(member => member.GetCustomAttributes(typeof(DescriptionAttribute), true).Cast<DescriptionAttribute>()))
            {
                Attributes.Add(attribute);
            }
        }
    }

Usage C#

class Program
    {
        public enum TestEnum
        {
            [Description("An apple a day keeps the doctor away")]
            Apple=0,
            [Description("Yummie Strawberry")]
            Strawberry=1,
            [Description("Banana for scale")]
            Banana=2
        }

        static void Main(string[] args)
        {
            var descriptions = new DescriptionAttributes<TestEnum>().Descriptions.ToList();
            foreach (var description in descriptions)
            {
                Console.WriteLine(description);
            }
            Console.Read();
        }
    }

Sample VB.NET – Class

Public Class DescriptionAttributes(Of T)
	Protected Attributes As New List(Of DescriptionAttribute)()
	Public Property Descriptions() As List(Of String)
		Get
			Return m_Descriptions
		End Get
		Set
			m_Descriptions = Value
		End Set
	End Property
	Private m_Descriptions As List(Of String)

	Public Sub New()
		RetrieveAttributes()
		Descriptions = Attributes.[Select](Function(x) x.Description).ToList()
	End Sub

	Private Sub RetrieveAttributes()
		For Each attribute As var In GetType(T).GetMembers().SelectMany(Function(member) member.GetCustomAttributes(GetType(DescriptionAttribute), True).Cast(Of DescriptionAttribute)())
			Attributes.Add(attribute)
		Next
	End Sub
End Class

Usage VB.NET

Class Program
	Public Enum TestEnum
		<Description("An apple a day keeps the doctor away")> _
		Apple = 0
		<Description("Yummie Strawberry")> _
		Strawberry = 1
		<Description("Banana for scale")> _
		Banana = 2
	End Enum

	Private Shared Sub Main(args As String())
		Dim descriptions = New DescriptionAttributes(Of TestEnum)().Descriptions.ToList()
		For Each description As var In descriptions
			Console.WriteLine(description)
		Next
		Console.Read()
	End Sub
End Class

Result / Output

Enum Descriptions

Enum Descriptions

Please rate this snippet

How to use DebuggerDisplayAttribute in C# and VB.NET

If you want to change the information, the debugger variable window shows for your class, you can use the DebuggerDisplayAttribute.
Simply put the Attribut above your class declaration. The Value between {} are the name of the property or Field you want to show.

Sample C#

[DebuggerDisplay("Name = {Name}, Value = {Value}")]

Sample VB.NET

<DebuggerDisplay("Name = {Name}, Value = {Value}")>

Result

DebuggerDisplayAttribute

for more informations see the DebuggerDisplayAttribute Class MSDN Entry

Please rate this snippet

How to get Enum by Descritption Attribute in C# and VB.NET

To get Enum by Descritption Attribute in C# and VB.NET you can use the following snippet.

Sample C#

public static T GetEnumFromDescriptionAttribute<T>(string description)
{
	var type = typeof(T);
	if (!type.IsEnum) throw new InvalidOperationException();
	foreach (var fieldInfo in type.GetFields())
	{
		var descriptionAttribute = Attribute.GetCustomAttribute(fieldInfo, typeof(DescriptionAttribute)) as DescriptionAttribute;
		if (descriptionAttribute != null)
		{
			if (descriptionAttribute.Description != description) continue;
			return (T)fieldInfo.GetValue(null);
		}
		if (fieldInfo.Name != description) continue;
		return (T)fieldInfo.GetValue(null);
	}
	return default(T);
}

Sample VB.NET

Public Shared Function GetEnumFromDescriptionAttribute(Of T)(description As String) As T
	Dim type = GetType(T)
	If Not type.IsEnum Then
		Throw New InvalidOperationException()
	End If
	For Each fieldInfo As var In type.GetFields()
		Dim descriptionAttribute = TryCast(Attribute.GetCustomAttribute(fieldInfo, GetType(DescriptionAttribute)), DescriptionAttribute)
		If descriptionAttribute IsNot Nothing Then
			If descriptionAttribute.Description <> description Then
				Continue For
			End If
			Return DirectCast(fieldInfo.GetValue(Nothing), T)
		End If
		If fieldInfo.Name <> description Then
			Continue For
		End If
		Return DirectCast(fieldInfo.GetValue(Nothing), T)
	Next
	Return Nothing
End Function

Please rate this snippet

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

Please rate this snippet

How to mark classes, methods and properties as Obsolete / Deprecated in c# or vb.net

How can a class marked as Deprecated/Obsolete in c# or vb.NET?
You can mark a class, method or property by adding the Obsolete Attribute above the declaration.

 

Sample C#

[Obsolete("This class should not be used anymore!",true)]
public class MyObsoleteClass
{
	...
}

Sample VB.NET

<Obsolete("This class should not be used anymore!", True)> _
Public Class MyObsoleteClass
	...
End Class

Tip

The second parameter of the attribute indicates whether the obsolete Class/Method/Property element usage is considered as error or not.

 

 

for more information see the MSDN about ObsoleteAttribute Class

Please rate this snippet