以下のコードで助けが必要です。基本的に私はいくつかの公共のフィールドを持っている "ジョブ"と呼ばれるクラスを持っています。私は自分のメソッド "ApplyFilter"に2つのパラメータ "job_in"と "job_filters"を渡しています。最初のパラメータには実際のデータが含まれ、2番目のパラメータには命令があります(存在する場合)。 "job_in"オブジェクトを繰り返し処理し、データを読み込み、 "job_filters"を読んで指示を適用し、必要に応じてデータを修正し、新しい "job_out"オブジェクトに返します。私は "job_out" オブジェクトに私のデータを格納する必要がありますまで、すべてが正常に動作します:C#。リフレクションを使用してメンバオブジェクトの値を設定する
public class Job
{
public string job_id = "";
public string description = "";
public string address = "";
public string details = "";
}
...
private Job ApplyFilters(Job job_in, Job job_filters)
{
Type type = typeof(Job);
Job job_out = new Job();
FieldInfo[] fields = type.GetFields();
// iterate through all fields of Job class
for (int i = 0; i < fields.Length; i++)
{
List<string> filterslist = new List<string>();
string filters = (string)fields[i].GetValue(job_filters);
// if job_filters contaisn magic word "$" for the field, then do something with a field, otherwise just copy it to job_out object
if (filters.Contains("$"))
{
filters = filters.Substring(filters.IndexOf("$") + 1, filters.Length - filters.IndexOf("$") - 1);
// MessageBox.Show(filters);
// do sothing useful...
}
else
{
// this is my current field value
var str_value = fields[i].GetValue(job_in);
// this is my current filed name
var field_name = fields[i].Name;
// I got stuck here :(
// I need to save (copy) data "str_value" from job_in.field_name to job_out.field_name
// HELP!!!
}
}
return job_out;
}
を助けてください。私はプロパティを使っていくつかのサンプルを見てきましたが、フィールドでも同じことをすることは可能でしょうか。ありがとう!
ビンゴをお試しください!魅力のように動作します。私はそれが何か簡単だと分かっていました!ありがとうございました! :) – Gary