1

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;
    }
}
Cornel
  • 72
  • 1
  • 7
  • 2
    `FormResponseAttribute` is not associated with `y` but it is associated with the method `Minus`. So you can not get information about the attribute from `y`. Since you are able to calls `calc.Minus` you can get the attribute information for that method using reflection. – Chetan Jan 16 '19 at 03:49
  • The idea looks wierd so better to redesign everything now than later. It can take some time, but it's very easy – opewix Jan 16 '19 at 03:54
  • The answer to this question likely doesn't matter, but just in case: what kind of information are you trying to pass and why? There could be an alternative solution that goes in an entirely different direction. – Scott Hannen Jan 16 '19 at 03:56
  • @ScottHannen Form validation data as well as a success status message. I want to do this to avoid having to wrap the current return type in an object and add these to it. I don't want this information necessarily display to a client. – Cornel Jan 16 '19 at 04:26
  • Possible duplicate of [Change Attribute's parameter at runtime](https://stackoverflow.com/questions/51269/change-attributes-parameter-at-runtime). – Streamline Jan 16 '19 at 06:48

0 Answers0