This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
namespace CSharp_Attributes_Walkthrough.My_Custom_Attributes | |
{ | |
[Alias("Filipino_Customers", ConsoleColor.Yellow)] | |
public class Customer | |
{ | |
[Alias("Fname", ConsoleColor.White, AlternativeName = "Customer_FirstName")] | |
public string Firstname { get; set; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using CSharp_Attributes_Walkthrough.My_Custom_Attributes; | |
using System; | |
namespace Implementing_Csharp_Attributes_101 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var customer = new Customer { Firstname = "Jin Vincent" , LastName = "Necesario" }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Fact] | |
public void Test_GetAll_AttributeTargets() | |
{ | |
var targets = Enum.GetNames(typeof(AttributeTargets)); | |
foreach (var target in targets) | |
{ | |
this._output.WriteLine($"AttributeTargets.{target}"); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using CSharp_Attributes_Walkthrough.My_Custom_Attributes; | |
using System; | |
namespace CSharp_Attributes_Walkthrough.My_Custom_Attributes | |
{ | |
public class AliasAttribute : Attribute | |
{ | |
//.... | |
//Added an optional-parameter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace CSharp_Attributes_Walkthrough.My_Custom_Attributes | |
{ | |
public class AliasAttribute : Attribute | |
{ | |
/// <summary> | |
/// These parameters will become mandatory once have you decided to use this attribute. | |
/// </summary> | |
/// <param name="alias"></param> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace CSharp_Attributes_Walkthrough.My_Custom_Attributes | |
{ | |
public class AliasAttribute : Attribute | |
{ | |
//This is how to define a custom attributes. | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace CSharp_Attributes_Walkthrough.My_Custom_Attributes | |
{ | |
/// <summary> | |
/// This class is a custom attribute class. | |
/// Moreover, it is using the AttributeUsage attribute to annotate | |
/// that this attribute is applicable only to class, struct and property. | |
/// </summary> | |
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Property)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Fact] | |
public void Test_Read_Attributes() | |
{ | |
//get the Product-class | |
var type = typeof(Product); | |
//Get the attributes of the Product-class and we are expecting the [Serializable] | |
var attribute = (SerializableAttribute)type. | |
GetCustomAttributes(typeof(SerializableAttribute), false).FirstOrDefault(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
namespace CSharp_Attributes_Walkthrough.My_Custom_Attributes | |
{ | |
[Serializable] | |
public class Product | |
{ | |
public string Name { get; set; } | |
public string Code { get; set; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Fact] | |
public void Test_GetAll_BuiltIn_Attributes() | |
{ | |
var assembly = Assembly.Load(assemblyFullName); | |
var attributes = assembly | |
.DefinedTypes | |
.Where(type => | |
type | |
.IsSubclassOf(typeof(Attribute))); |