Csharp/CSharp Tutorial/GUI Windows Forms/Binder — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 15:31, 26 мая 2010
extends Binder to create your own binder
using System;
using System.Reflection;
using System.Globalization;
class MyClass
{
DateTime[] dateTimes = new DateTime[10];
public DateTime this[int index]
{
get{ return dateTimes[index]; }
set{ dateTimes[index] = value;}
}
private DateTime dateOfBirth;
public DateTime DateOfBirth
{
get{ return dateOfBirth; }
set{ dateOfBirth = value; }
}
public void Test()
{
Console.WriteLine("Test method called");
}
private string field;
public string Property
{
get{ return field; }
set{ field = value; }
}
}
class MainClass{
static void Main(string[] args)
{
Type type = typeof(MyClass);
object o = Activator.CreateInstance(type);
type.InvokeMember( "DateOfBirth",
BindingFlags.Instance | BindingFlags.SetProperty |
BindingFlags.Public, new DateTimeBinder(),
o, new object[]{@"02/12/1966"});
Console.WriteLine(((MyClass)o).DateOfBirth);
}
}
public class DateTimeBinder : Binder
{
public override MethodBase BindToMethod(
BindingFlags bindingAttr,
MethodBase[] match,
ref object[] args,
ParameterModifier[] modifiers,
CultureInfo culture,
string[] names,
out object state)
{
state = null;
return match.Length == 1 ? match[0] : null;
}
public override FieldInfo BindToField(
BindingFlags bindingAttr,
FieldInfo[] match,
object value,
CultureInfo culture)
{
return null;
}
public override MethodBase SelectMethod(
BindingFlags bindingAttr,
MethodBase[] match,
Type[] types,
ParameterModifier[] modifiers)
{
return null;
}
public override PropertyInfo SelectProperty(BindingFlags bindingAttr,PropertyInfo[] match,Type returnType,Type[] indexes,ParameterModifier[] modifiers){
return null;
}
public override object ChangeType(object value,Type type,CultureInfo culture)
{
if ( value.GetType() != typeof(string)) return value;
try
{
return Convert.ToDateTime(value);
}
catch
{
return value;
}
}
public override void ReorderArgumentArray(ref object[] args,object state){}
}
extends ISupportInitialize
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form1
{
static void Main()
{
BindingSource bindSource = new BindingSource();
MyClass dataContainer = new MyClass();
ISupportInitialize bindSourceInit = bindSource;
ISupportInitialize dataInit = dataContainer;
bindSourceInit.BeginInit();
dataInit.BeginInit();
dataContainer.StringCollection = new List<string>();
bindSource.DataSource = dataContainer;
bindSource.DataMember = "StringCollection";
bindSourceInit.EndInit();
dataInit.EndInit();
}
}
public class MyClass : ISupportInitialize
{
private List<string> m_Data = null;
private bool m_Initializing = false;
private List<string> m_TempData = null;
public List<string> StringCollection
{
get
{
return m_Data;
}
set
{
if (m_Initializing)
m_TempData = value;
else
m_Data = value;
}
}
void ISupportInitialize.BeginInit()
{
m_Initializing = true;
}
void ISupportInitialize.EndInit()
{
m_Data = m_TempData;
m_Initializing = false;
}
}