/// <summary>
/// 특정클래스의 field값을 변경하는 함수
/// ex) m_PLC_Data.Field_Write(m_PLC_Data, "posX",100);
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="exm"></param>
/// <param name="valueName"></param>
/// <param name="subject"></param>
public void Field_Write<T>(T exm, string valueName, object subject)
{
//Type tp = typeof(T);
Type tp = exm.GetType();
FieldInfo[] flds = tp.GetFields(BindingFlags.Instance |
BindingFlags.NonPublic |
BindingFlags.Public |
BindingFlags.NonPublic);
foreach (var t in flds)
{
if (valueName != t.Name)
continue;
object point = t.GetValue(exm);
if (point is int)
t.SetValue(exm, int.Parse(subject.ToString()));
if (point is float)
t.SetValue(exm, float.Parse(subject.ToString()));
if (point is string)
t.SetValue(exm, subject);
}
}
/// <summary>
/// 특정 클래스의 값을 읽어오는 함수
/// ex) Console.WriteLine(m_PLC_Data.Field_GetValue(m_PLC_Data, "posX"));
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="exm"></param>
/// <param name="valueName"></param>
/// <returns></returns>
public object Field_GetValue<T>(T exm, string valueName)
{
Type tp = exm.GetType();
FieldInfo[] flds = tp.GetFields(BindingFlags.Instance |
BindingFlags.NonPublic |
BindingFlags.Public |
BindingFlags.NonPublic);
object point = null;
foreach (var t in flds)
{
if (valueName != t.Name)
continue;
point = t.GetValue(exm);
break;
}
return point;
}
댓글 없음:
댓글 쓰기