开发者

how to assign one class properties values to otehr class properties values

开发者 https://www.devze.com 2022-12-30 13:30 出处:网络
I have a class public St开发者_Python百科udentstudent {get;set;} public Students students {get;set;}

I have a class

public St开发者_Python百科udent  student {get;set;}

public Students students {get;set;}

both class properties are same.. I am getting student values from data base.. I need to assign those values to students.. can anybody tell me how to do this?


Do you need help: 1) Assigning to the various properties of the classes, or 2) obtaining the data from the database?

Here's a summary:

To populate the properties of your class from SQL (or MySQL or Oracle, etc.) you need to create a SqlConnection, a SqlCommand, and a SqlDataReader. Using the connection, write the query (command) such that you obtain the data you want. Then, use the reader in a while loop to read the values from the result set, and populate your class.

Each class should have properties such as strings, ints, etc. that you simply set like variables:

student.Name = Reader["name"].Equals(DBNull.Value ?? "" : (string)Reader["name"];

If you need more help, post the definition of your class and maybe some more info about the database that you want to populate the class with (such as example table structure and data).


You could do something like this: http://gist.github.com/405432

It uses reflection to do a deep memberwise clone on an object:

    static void Main(string[] args) {
        foreach (var student in Students) 
            student.CloneFrom(Student);
    }

    public static void CloneFrom<T>(this T ToObj, T FromObj) {
        CloneFrom(ToObj, FromObj, new Dictionary<object, object>());
    }

    private static FieldInfo[] GetMemberFields(this Type type) {
        return type.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
    }

    private static void CloneFrom<T>(T ToObj, T FromObj, Dictionary<object, object> Values) {
        Values.Add(FromObj, ToObj); //The dictionary is used to prevent infinite recursion

        var fields = ToObj.GetType().GetMemberFields();
        object value = null;
        object othervalue = null;
        foreach (var field in fields) {
            value = field.GetValue(FromObj);
            othervalue = field.GetValue(ToObj);
            if (object.Equals(othervalue, value)) continue;

            if (value != null && Values.ContainsKey(value)) {
                //Prevent recursive calls: objA.fieldB.fieldC = objA

                field.SetValue(ToObj, Values[value]);
            } else if (othervalue != null && value != null && !field.FieldType.IsPrimitive && !field.FieldType.IsValueType && !(value is string)
                && field.FieldType.GetMemberFields().Any()) {

                //Do not overwrite object references
                CloneFrom(othervalue, value, Values);
            } else {
                field.SetValue(ToObj, value);
            }
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号