【问题标题】:Get all properties from a view model to display in array从视图模型中获取所有属性以显示在数组中
【发布时间】:2016-09-09 17:10:38
【问题描述】:

我有一个有很多属性的视图模型,我创建了一个自定义属性来将数据发送到 hubspot,因为 hubspot 需要一个特定的命名法,然后我需要创建一个具有一些迭代器之王的方法,即对于包含我的自定义属性的每个属性,他都放置了一个特定的输出,这里是代码:

public class CreateTrialUserHubspotViewModel {

    [HubspotAttribute("firstname")]   
    [Display(Name = "First Name")]
    [StringLength(50)]
    [Required]
    public string FirstName { get; set; }

    [HubspotAttribute("lastname")]
    [Display(Name = "Last Name")]
    [StringLength(50)]
    [Required]
    public string LastName { get; set; }

    [HubspotAttribute("jobtitle")]
    [Display(Name = "Job title")]
    [StringLength(50)]
    [Required]
    public string JobTitle { get; set; }
}

现在这是我的自定义属性

[AttributeUsage(AttributeTargets.All)]
public class HubspotAttribute : System.Attribute {
    public readonly string hubspotValue;

    public HubspotAttribute(string value)
    {
        this.hubspotValue = value;
    }
}

然后我需要创建一个方法来获取视图模型对象并创建我的输出,我需要一些关于如何做到这一点的建议,将是这样的:

private static RowValidation ValidateRowWithManifest<T>(CreateTrialUserHubspotViewModel trialUser) {
        RowValidation validation = new RowValidation();

        FieldInfo[] fields = typeof(T).GetPropertiesOfSomeWay;

        foreach (DataType field in fields) {
           output+=whatINeed
        }
        return validation;
    }
}

所需的输出将类似于:[firstname:"pepe", lastname="perez", jobtitle"none"]。只需调用该方法将返回我需要的所有数据。

【问题讨论】:

  • 您的数据输出甚至没有意义。您有冒号等号,并且没有键/值对的表示器。你真正需要的是什么?
  • 让我更好地解释一下,我有属性,有名称(当然),但我添加了一些自定义属性,以获得每个道具所需的确切名称,所以我需要一个方法期望一个 viewmodel 对象,然后他为每个 prop.customattr + ":" + viewModelObject.value 构建一个字符串。告诉我你现在是否理解得更好

标签: c# asp.net orchardcms hubspot


【解决方案1】:
 public string GetString<T>(T @object)
        {
            var output = new StringBuilder();
            var type = typeof(T);
            var properties = type.GetProperties();
            foreach (var property in properties)
            {
                var attributes = property.GetCustomAttributes(typeof(HubspotAttribute), true);
                if (attributes.Length == 0)
                    continue;

                var name = ((HubspotAttribute)attributes[0]).hubspotValue;
                var value = property.GetValue(@object) ?? "none";
                output.AppendFormat("{0}:\"{1}\",", name, value);
            }

            var fields = output.ToString().TrimEnd(',');
            return string.Format("[{0}]", fields);

        }

【讨论】:

  • 不够通用,你被绑定到一个特定的 HubspotAttribute
【解决方案2】:

如果您正在寻找将属性连接成 看起来 像 JSON 字符串的字符串(这是处理它的更好方法),您可以使用如下内容:

private static string CreateOutput(CreateTrialUserHubspotViewModel trialUser)
{
    var properties = trialUser.GetType().GetProperties().Where(x => Attribute.IsDefined(x, typeof(HubspotAttribute))).ToList();

    var values = properties.Select(x =>
    {
        var att = x.GetCustomAttribute(typeof(HubspotAttribute));
        var key = ((HubspotAttribute)att).hubspotValue;
        var val = x.GetValue(trialUser);
        return $"{key}:{val}";
    });

    var sb = new StringBuilder();
    values.ToList().ForEach(v =>
    {
        sb.Append(v);
        if (values.Last() != v) sb.Append(',');
    });

    return sb.ToString();
}

【讨论】:

  • 是的,类似的,谢谢。我是 C# 新手。我曾经是一个 java 人,又是 ty。
  • @dayanrr91 没问题。就像我说的,你的里程可能会通过研究 JSON 序列化来提高,但这至少会让你开始你需要的东西:)。
猜你喜欢
  • 2020-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-24
  • 1970-01-01
  • 1970-01-01
  • 2012-01-10
  • 2020-09-06
相关资源
最近更新 更多