In my program (section 1) I call a function from a class (section 2)that returns a value with a custom attribute (section 3) attached. The issue I am having is that I am trying to access the Attribute returned from the Minus
function the in the calculator class.
I am trying to pass information to and from functions by embedding attributes with information. I do not want to pass variables in the inputs or outputs of functions since I am trying to avoid massive reworks on functions.
Section 1 - Main Program
class Program
{
static void Main(string[] args)
{
Calculator calc = new Calculator();
var y = calc.Minus(1, 2);
// Write the value
Console.WriteLine(y);
// Write the attribute value associated with variable `y`
// - Unsure how to do this - <----- ISSUE AREA
// Read key
Console.WriteLine();
}
}
Section 2 - Function Attribute received from
public class Calculator {
[FormResponse(HiddenMessage = "default")]
public decimal Minus(decimal a, decimal b)
{
MethodBase.GetCurrentMethod().SetMessage("Success");
Console.WriteLine(MethodBase.GetCurrentMethod().GetMessage());
return a - b;
}
}
Section 3 - Custom Attribute
/// <summary>
/// Form Response
/// </summary>
[AttributeUsage(AttributeTargets.All)]
public class FormResponseAttribute : Attribute
{
/// <summary>
/// Forms
/// </summary>
public string HiddenMessage { get; set; }
}
The code below is not critical to the issue, but is posted here for insight for those interested.
Section 4 - Supporting Methods
public static class FormResponseFunctions
{
/// <summary>
/// </summary>
/// <param name=""></param>
/// <param name="forms"></param>
/// <param name=""></param>
public static void SetMessage(this MethodBase method, string s)
{
var attrib = method.GetCustomAttributes(typeof(FormResponseAttribute), true);
var attributeProperties = (FormResponseAttribute)attrib[0];
attributeProperties.HiddenMessage = s;
}
/// <summary>
/// </summary>
/// <param name=""></param>
/// <param name="forms"></param>
/// <param name=""></param>
public static string GetMessage(this MethodBase method)
{
var attrib = method.GetCustomAttributes(typeof(FormResponseAttribute), true);
var attributeProperties = (FormResponseAttribute)attrib[0];
return attributeProperties.HiddenMessage;
}
}