C
C#2y ago
SWEETPONY

✅ What should I do with the attributes?

public class Arguments
{
[Component(InputType.DepartmentsTreeSelect)]
[ComponentOptions()]
[JsonProperty(PropertyName = "departments_ids")]
public HashSet<string>? DepartmentsIDs { get; set; }
}
public class Arguments
{
[Component(InputType.DepartmentsTreeSelect)]
[ComponentOptions()]
[JsonProperty(PropertyName = "departments_ids")]
public HashSet<string>? DepartmentsIDs { get; set; }
}
can you give advice on how to do it better? I just can't think of... here I have a field over which custom attributes are defined with the Component attribute, there are no problems, we just pass a string that indicates which component to draw but with ComponentOptions, everything is more complicated, because the options are a separate class and I would like the user to fill it in, but since we cannot pass it to the attribute class, then I do not know what to do And this is possible class for ComponentOptions but I can't use it in attribute
public class ExtendedProperties
{
/// <summary>
/// UI component type
/// </summary>
[JsonProperty(PropertyName = "inputType")]
public InputType InputType { get; set; }

/// <summary>
/// defines order
/// </summary>
[JsonProperty(PropertyName = "order")]
public int Order { get; set; }

/// <summary>
/// defines multiple selection for UI component
/// </summary>
[JsonProperty(PropertyName = "multiple")]
public bool Multiple { get; set; }

/// <summary>
/// defines options
/// </summary>
[JsonProperty(PropertyName = "options")]
public Options? Options { get; set; }

/// <summary>
/// defines component properties
/// </summary>
[JsonProperty(PropertyName = "componentProps")]
public ComponentProperties? ComponentProperties { get; set; }
}
public class ExtendedProperties
{
/// <summary>
/// UI component type
/// </summary>
[JsonProperty(PropertyName = "inputType")]
public InputType InputType { get; set; }

/// <summary>
/// defines order
/// </summary>
[JsonProperty(PropertyName = "order")]
public int Order { get; set; }

/// <summary>
/// defines multiple selection for UI component
/// </summary>
[JsonProperty(PropertyName = "multiple")]
public bool Multiple { get; set; }

/// <summary>
/// defines options
/// </summary>
[JsonProperty(PropertyName = "options")]
public Options? Options { get; set; }

/// <summary>
/// defines component properties
/// </summary>
[JsonProperty(PropertyName = "componentProps")]
public ComponentProperties? ComponentProperties { get; set; }
}
11 Replies
Pobiega
Pobiega2y ago
attributes need to be compile time constant, so yeah you can't use classes
SWEETPONY
SWEETPONYOP2y ago
it's a problem 😦 I need following logic for user: "hm, I need to create DepartmentsTreeSelect so I will use InputType.DepartmentsTreeSelect in ComponentAttribute than.. I need to define some properties for this component. Hm, okey let's add some properties like order etc to ComponentOptions
Pobiega
Pobiega2y ago
then do that?
public class Arguments
{
[Component(InputType.DepartmentsTreeSelect)]
[ComponentOptions(Order = 12, Multiple = true)]
[JsonProperty(PropertyName = "departments_ids")]
public HashSet<string>? DepartmentsIDs { get; set; }
}
public class Arguments
{
[Component(InputType.DepartmentsTreeSelect)]
[ComponentOptions(Order = 12, Multiple = true)]
[JsonProperty(PropertyName = "departments_ids")]
public HashSet<string>? DepartmentsIDs { get; set; }
}
you cant have a complicated nested structure in your options, it needs to be flat
SWEETPONY
SWEETPONYOP2y ago
and what is Order in your case? it is a property inside a class?
Pobiega
Pobiega2y ago
its a property inside the attribute class in general this shouldnt be its own attribute imho, it should be part of Component
SWEETPONY
SWEETPONYOP2y ago
idk.. how it will be look?
Pobiega
Pobiega2y ago
public class Arguments
{
[Component(InputType.DepartmentsTreeSelect, Order = 2, Multiple = false)]
public HashSet<string> DepartmentIds { get; set; }
}

public class ComponentAttribute : Attribute
{
public ComponentAttribute(InputType inputType)
{
InputType = inputType;
throw new NotImplementedException();
}

public InputType InputType { get; }

public int Order { get; set; }

public bool Multiple { get; set; }
}
public class Arguments
{
[Component(InputType.DepartmentsTreeSelect, Order = 2, Multiple = false)]
public HashSet<string> DepartmentIds { get; set; }
}

public class ComponentAttribute : Attribute
{
public ComponentAttribute(InputType inputType)
{
InputType = inputType;
throw new NotImplementedException();
}

public InputType InputType { get; }

public int Order { get; set; }

public bool Multiple { get; set; }
}
SWEETPONY
SWEETPONYOP2y ago
hm interesting I will try to do smth the same thanks
Pobiega
Pobiega2y ago
give it a try.
SWEETPONY
SWEETPONYOP2y ago
public class ComponentAttribute : Attribute
{
public ComponentAttribute(InputType inputType)
{

}

public int Order => Properties.Order;

public ExtendedProperties Properties { get; set; } = new();
}
public class ComponentAttribute : Attribute
{
public ComponentAttribute(InputType inputType)
{

}

public int Order => Properties.Order;

public ExtendedProperties Properties { get; set; } = new();
}
what do you think about this? I do this because I need to use properties from defined class
Pobiega
Pobiega2y ago
That won't work, Order is readonly there.
Want results from more Discord servers?
Add your server