PDA

View Full Version : سوال: ايجاد يك شي جديد با داشتن خصوصيات ش موجود



bill_geits
شنبه 30 خرداد 1388, 12:33 عصر
با سلام

چطوري ميشه يك شي از كلاس datagridview ايجاد كرد كه تمام خصوصياتش مثل رنگ يا استايلش رو از يك datagridview موجود ارث بري كنه و آدرسش فرق كنه. ؟؟ :عصبانی++:

seniamail
شنبه 30 خرداد 1388, 12:49 عصر
سلام
نمونه کد برای ایجاد Button

// Add Button
Button MyButton = new Button();
MyButton.Text = "Press Here";
MyButton.Location = new Point(100, 200);
Controls.Add(MyButton);
//

bill_geits
شنبه 30 خرداد 1388, 16:21 عصر
دوست عزيز منظور من اين نبود !!!

من ميخوام از يك شي كپي بگيرم به طوري كه با تغيير در يكي , شي ديگر تغيير نكند.؟ :عصبانی++:

adinochestva
شنبه 30 خرداد 1388, 17:10 عصر
با serialize و deserialize كردن مي توني


public static class SerializationUtil { static public T Clone<T>(T source) { Debug.Assert(typeof(T).IsSerializable); IGenericFormatter formatter = new GenericBinaryFormatter( ); Stream stream = new MemoryStream( ); using(stream) { formatter.Serialize(stream,source); stream.Seek(0,SeekOrigin.Begin); T clone = formatter.Deserialize<T>(stream); return clone; } } //Rest of SerializationUtil }

bill_geits
شنبه 30 خرداد 1388, 21:04 عصر
جناب adinochestva از اينكه جوابمو دادين ازتون ممنونم. هنوز تستش نكردم ولي حتما جواب ميده. انشاالله.

esmaeily-hosein
یک شنبه 31 خرداد 1388, 12:02 عصر
public static object Clone(this Control control)
{
Type t = typeof(control);
System.Reflection.PropertyInfo[] properties = t.GetProperties();

Object p = t.InvokeMember("", System.Reflection.BindingFlags.CreateInstance, null, gridColumn, null);

foreach (PropertyInfo pi in properties)
{
if (pi.CanWrite)
{
try
{
pi.SetValue(p, pi.GetValue(column, null), null);
}
catch(Exception){}
}
}
return p;
}