Avatar
0
Nguyễn Linh Beginner
Nguyễn Linh Beginner
Làm thế nào để lấy 3 field info như nội dung
public class AdminLoginByAccountRequestData : LoginByAccountRequestData
{
      [StringDataMember(GNParameterCode.Username, minLength = 6, maxLength = 32, mustNonNull = true)]
      public new string username;
}

public class LoginByAccountRequestData
{
      [StringDataMember(GNParameterCode.Username, minLength = 6, maxLength = 32, mustNonNull = true)]
      public string username;

      [StringDataMember(GNParameterCode.Password, minLength = 6, maxLength = 64, mustNonNull = true)]
      public string password;

      [StringDataMember (GNParameterCode.InfoRequestParam, mustNonNull = true)]
      public InfoRequestParam infoRequestParam;
}

làm thế nào để lấy được 3 field info có attribute StringDataMember, trong đó username là field của AdminLoginByAccountRequestData, còn password và infoRequestParam là field của LoginByAccountRequestData

  • Answer
Remain: 5
1 Answer
Avatar
tvd12 Beginner
tvd12 Beginner
The Best Answer
Anh đang tưởng tượng em có code C# thế này:
public enum GNParameterCode {
    Username,
    Password,
    InfoRequestParam
}

public class InfoRequestParam
{
}

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public class StringDataMemberAttribute : Attribute
{
    public GNParameterCode parameterCode;
    public int minLength;
    public int maxLength;
    public bool mustNonNull;

    public StringDataMemberAttribute(GNParameterCode parameterCode)
    {
        this.parameterCode = parameterCode;
    }

    public StringDataMemberAttribute(GNParameterCode parameterCode, int minLength, int maxLength, bool mustNonNull)
    {
        this.parameterCode = parameterCode;
        this.minLength = minLength;
        this.maxLength = maxLength;
        this.mustNonNull = mustNonNull;
    }
}

public class AdminLoginByAccountRequestData : LoginByAccountRequestData
{

    [StringDataMember(GNParameterCode.Username, minLength = 6, maxLength = 32, mustNonNull = true)]
    public new string username;

}

public class LoginByAccountRequestData
{
    [StringDataMember(GNParameterCode.Username, minLength = 6, maxLength = 32, mustNonNull = true)]
    public string username;

    [StringDataMember(GNParameterCode.Password, minLength = 6, maxLength = 64, mustNonNull = true)]
    public string password;

    [StringDataMember(GNParameterCode.InfoRequestParam, mustNonNull = true)]
    public InfoRequestParam infoRequestParam;
}

Vậy em có thể sử dụng reflection để truy cập các field như sau:

class Program
{
    public static void Main(string[] args)
    {

        // Create an instance of the class
        AdminLoginByAccountRequestData requestData = new AdminLoginByAccountRequestData();
        requestData.username = "Hello";
        requestData.password = "World";

        // Get the type of the class
        Type type = requestData.GetType();

        // Get all fields of the class
        FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);

        // Loop through the fields
        foreach (FieldInfo field in fields)
        {
            // Check if the field has the StringDataMemberAttribute
            if (Attribute.IsDefined(field, typeof(StringDataMemberAttribute)))
            {
                StringDataMemberAttribute attribute = (StringDataMemberAttribute)Attribute.GetCustomAttribute(field, typeof(StringDataMemberAttribute));

                // Access the attribute properties
                GNParameterCode parameterCode = attribute.parameterCode;
                int minLength = attribute.minLength;
                int maxLength = attribute.maxLength;
                bool mustNotBeNull = attribute.mustNonNull;

                Console.WriteLine($"Field: {field.Name} value: {field.GetValue(requestData)}");
                Console.WriteLine($"ParameterCode: {parameterCode}");
                Console.WriteLine($"MinLength: {minLength}");
                Console.WriteLine($"MaxLength: {maxLength}");
                Console.WriteLine($"MustNotBeNull: {mustNotBeNull}");
            }
        }
    }
}
  • 0
  • Reply