부모 개체 또는 사용자 개체의 사용자 지정 특성에 액세스하려면 어떻게해야합니까? SQLFieldInfo 구조체의 FieldInfo 속성을 살펴보십시오.C# 소유자 개체의 사용자 지정 특성 액세스
여기 내가 필요한 것을 보여주고 컴파일 할 더 자세한 프로그램이 있습니다.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Employee myclass = new Employee();
// Load from sql server...
myclass.Name = "Alain";
myclass.Age = 51;
//----
MessageBox.Show(myclass.Name.ToString()); // Should return Alain
MessageBox.Show(myclass.Age.FieldInfo.Type.ToString()); // Should output "int"
}
}
// This next class is generated by a helper exe that reads SQL table design and create the class from it
[SQLTableAttribute(DatabaseName = "Employees", Schema = "dbo", TableName = "Employees")]
public class Employee
{
[SQLFieldAttribute(FieldName = "ID", Type = SqlDbType.Int)]
public SQLFieldInfo<int> ID { get; set; }
[SQLFieldAttribute(FieldName = "Name", Type = SqlDbType.NVarChar, Size = 200)]
public SQLFieldInfo<String> Name { get; set; }
[SQLFieldAttribute(FieldName = "Age", Type = SqlDbType.Int)]
public SQLFieldInfo<int> Age { get; set; }
}
public struct SQLFieldInfo<T>
{
private readonly T value;
public SQLFieldInfo(T Value)
{
this.value = Value;
}
public static implicit operator SQLFieldInfo<T>(T Value)
{
return new SQLFieldInfo<T>(Value);
}
public T Value
{
get
{
return this.value;
}
}
public override string ToString()
{
return this.value.ToString();
}
public SQLFieldAttribute FieldInfo
{
get
{
// Need to retreive the attribute class of the parent or declaring member
return null;
}
}
}
// Holds the sql field information
public class SQLFieldAttribute : Attribute
{
public string FieldName { get; set; }
public SqlDbType Type { get; set; }
public bool AllowNull { get; set; }
public int Size { get; set; }
}
// Holds the sql table information
public class SQLTableAttribute : Attribute
{
public string DatabaseName { get; set; }
public string Schema { get; set; } = "dbo";
public string TableName { get; set; }
}
감사합니다!
알랭
'B'는'A '안에 어떻게 포함되어 있는지 어떻게 알아야합니까? 나는'B.myprop2' 내부의 호출 스택을 검사하여'A.myprop'가 이전 프레임인지 여부를 확인할 수 있다고 생각합니다. 그러나 호출 스택의 레이아웃 종속성을 취하는 것이 문제를 요구합니다. 특히 릴리스 빌드에서 다른 레이아웃을 가질 수 있습니다. –
B가 그것에 대해 알기를 기대하지는 않지만 CLR 예에서 시스템이 작성자 인스턴스를 알고 리플렉션을 사용하여 사용자 정의 속성에 액세스하는 것이 쉬워야한다고 생각합니다. – user1603581