C#字符串变量的泛型参数

C#字符串变量的泛型参数,c#,generics,reflection,attributes,C#,Generics,Reflection,Attributes,我有两个班,客户和国家。Customer有一个名为HomeCountry的属性,我用一个名为“Lookup”的自定义属性修饰它,并使用一个字符串参数“Country”。其目的是,当我使用Customer类时,HomeCountry中的项必须存在于Country类中(这恰好是一个列表) 我使用反射来迭代Customer类,它会找到属性,我希望它检查country项列表中的值。到目前为止,我已经: foreach (PropertyInfo _pi in object.GetType().GetPr

我有两个班,客户和国家。Customer有一个名为HomeCountry的属性,我用一个名为“Lookup”的自定义属性修饰它,并使用一个字符串参数“Country”。其目的是,当我使用Customer类时,HomeCountry中的项必须存在于Country类中(这恰好是一个列表)

我使用反射来迭代Customer类,它会找到属性,我希望它检查country项列表中的值。到目前为止,我已经:

foreach (PropertyInfo _pi in object.GetType().GetProperties()) {
  IEnumerable<Attribute> _attrs = _pi.GetCustomAttributes();
  foreach (Attribute _a in _attrs) {
    Object obj = Activator.CreateInstance(_type, null);
    // what goes here?
  }
}
foreach(object.GetType().GetProperties()中的PropertyInfo\u pi){
IEnumerable_attrs=_pi.GetCustomAttributes();
foreach(属性在属性中){
Object obj=Activator.CreateInstance(_-type,null);
//这里有什么?
}
}
我有一个方法:

public T Populate<T>(params string[] _parameters)
public T填充(参数字符串[]\u参数)
我想我想做些什么

List<obj> v = populate<obj>();
List v=populate();

List v=populate();
但显然什么都不管用!有人能帮我吗


谢谢

好的,我将尝试提供一个完整的示例:

我有一个客户订单类:

public class CUSTOMER_ORDER {
  public CUSTOMER_ORDER() {}

  [Key(0)]
  public string OrderNumber {get;set;}
  public MPCCOM_SHIP_VIA ShipVia {get;set;}
}
然后,MPCCOM\u通过类进行装运:

public class MPCCOM_SHIP_VIA {
  public MPCCOM_SHIP_VIA() {}

  [Key(0)]
  public string ID {get;set;}
  public string Description {get;set;}
}
我有一个名为Populate的方法,它接受一个类,然后使用反射循环所有属性,构建一个select语句,执行它,然后返回数据并填充对象:

public T Populate<T>(params string[] @Parameters)
        {
            Type _t = typeof(T);
            dynamic _o = Activator.CreateInstance(typeof(T), null);

            SqlBuilder _sb = new SqlBuilder();
            _sb.Table = string.Format("{0}.{1}", _Owner, _t.Name.ToString());
            foreach (PropertyInfo p in _t.GetProperties(Utilities.BindingFlags))
            {
                if (p.GetMethod.IsPrivate == false) _sb.Fields.Add(p.Name.ToString());
                IEnumerable<Attribute> _attrs = p.GetCustomAttributes();
                foreach (Attribute _a in _attrs)
                {
                    if (_a.TypeId.ToString().Equals(typeof(Key).FullName))
                    {
                        int _position = ((Key)_a).Position;
                        try
                        {
                            string _parameter = @Parameters[_position];
                            _sb.Where.Add(string.Format("{0} = '{1}'", p.Name, _parameter));
                        }
                        catch {}
                    }
                }
            }

            using (OleDbCommand _cmd = new OleDbCommand())
            {
                _cmd.Connection = this._cn;
                _cmd.CommandText = _sb.SQL;
                if (_trn != null) _cmd.Transaction = _trn;
                _cmd.CommandType = System.Data.CommandType.Text;
                using (OleDbDataReader _reader = _cmd.ExecuteReader())
                {
                    if (_reader.Read())
                    {
                        for (int x = 0; x < _reader.FieldCount; x++)
                        {
                            foreach (PropertyInfo p in _t.GetProperties(Utilities.BindingFlags))
                            {

                                if (p.GetMethod.IsPrivate == false)
                                {
                                    if (p.Name.Equals(_reader.GetName(x).ToString()))
                                    {
                                        dynamic _val = _reader.GetValue(x);
                                        if (p.ReflectedType.BaseType.Name.Equals(""))
                                        {
                                            // what goes here!

                                        }
                                        try
                                        {
                                            p.GetSetMethod(true).Invoke(_o, new object[] { _val });
                                        }
                                        catch { }
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        throw new DatabaseObjectNotFound(_t.Name.ToString(), string.Join(",",@Parameters));
                    }
                }
            }
            return (T)_o;

        }
public T填充(参数字符串[]@Parameters)
{
类型_t=类型(t);
dynamic _o=Activator.CreateInstance(typeof(T),null);
SqlBuilder _sb=新的SqlBuilder();
_sb.Table=string.Format(“{0}.{1}”,_Owner,_t.Name.ToString());
foreach(PropertyInfo p in_t.GetProperties(Utilities.BindingFlags))
{
如果(p.GetMethod.IsPrivate==false)_sb.Fields.Add(p.Name.ToString());
IEnumerable_attrs=p.GetCustomAttributes();
foreach(属性在属性中)
{
if(_a.TypeId.ToString().Equals(typeof(Key.FullName))
{
int _position=((键)a).position;
尝试
{
字符串_参数=@Parameters[_位置];
_sb.Where.Add(string.Format(“{0}='{1}',p.Name,_参数));
}
捕获{}
}
}
}
使用(OleDbCommand _cmd=new OleDbCommand())
{
_cmd.Connection=this.\u cn;
_cmd.CommandText=_sb.SQL;
如果(\u trn!=null)\u cmd.Transaction=\u trn;
_cmd.CommandType=System.Data.CommandType.Text;
使用(OleDbDataReader\u reader=\u cmd.ExecuteReader())
{
if(_reader.Read())
{
对于(int x=0;x<\u reader.FieldCount;x++)
{
foreach(PropertyInfo p in_t.GetProperties(Utilities.BindingFlags))
{
if(p.GetMethod.IsPrivate==false)
{
if(p.Name.Equals(_reader.GetName(x.ToString()))
{
动态_val=_reader.GetValue(x);
if(p.ReflectedType.BaseType.Name.Equals(“”)
{
//这里有什么!
}
尝试
{
p、 GetSetMethod(true).Invoke(_o,新对象[]{u val});
}
捕获{}
打破
}
}
}
}
}
其他的
{
抛出新的DatabaseObjectNotFound(_t.Name.ToString(),string.Join(“,”,@Parameters));
}
}
}
返回(T)_o;
}

因此,当我读取订单时,源DB在相应字段中获取MPCCOM_SHIP_VIA的密钥,我想使用该密钥对MPCCOM_SHIP_VIA对象调用相同的填充方法。我希望这更能说明我想做什么。谢谢

在四处搜寻之后,这就是我一直在寻找的答案

MethodInfo method = typeof(class).GetMethod("Populate");
method = method.MakeGenericMethod(p.PropertyType);
_val = method.Invoke(class, new object[] { _prms });

我想我的问题是我问错了问题

考虑为您的国家/地区使用enum,这样您就不会使用反射来执行此类代码。您正在以一种非常复杂的方式做一件简单的事情。我不确定您的代码的哪个组件实际上包含您提到的列表。请举例说明如何使用这些类。如果使用枚举路径,请确保正确验证输入。任何自然数都将解析为一个枚举(假设它符合继承的数字类型)。请提供
Country
Customer
类的代码以及验证的上下文。我正在尝试制作一个有意义的示例。。。这些对象是从源数据库(我无法控制)填充的。populate当前接受一个作为主键的参数,并从源数据库读取该项,然后返回已填充的对象。我在这里尝试的是,如果数据库中的一个表是另一个表中的查找值,我想使用一个属性来标记该关系,然后在我尝试将该对象写入数据库之前,在执行databa之前检查查找是否合法
MethodInfo method = typeof(class).GetMethod("Populate");
method = method.MakeGenericMethod(p.PropertyType);
_val = method.Invoke(class, new object[] { _prms });