none
SL数据绑定-TFTable RRS feed

  • 常规讨论

  • -----------------------------------------------------------------------------------------------------------------
    正在开发一个TFTable,TFRow,TFField的类
    1)在后台可与ADO.net的DataTable相互转换。
    2)前台能够实现和SL元素的直接绑定
    3)Web端和SL端能够自动系列化、反系列化
    4)支持.net基本数据类型(文本、数字、日期、布尔、二进制,二进制主要用来处理图片)
    5)计划SL端,可以向Web段发SQL语句(准备搞一个TFDBCommand,TFDBParameter,完全仿照ADO.net),返回TFTable,这样的话,就和我们用平常的App编程一样了,全部可以用SL(当然我正在考虑如何搞安全,毕竟访问服务器的东西,要考虑安全,安全部分,主要是和后台建立联系,我想就搞一个TFDBConnection,里面要求用户名称和密码)

    我正在书写,思路基本理顺了,先把TFTable发给大家看看,欢迎提出宝贵意见,或者共同完成

    ----------------------------------------------------------------------------------------------------------------

    1.1)Page.xaml

    <UserControl x:Class="SLTestX.Page"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

        <Grid x:Name="GrdRoot"
              Background="#BEDAFE"
              Width="500"
              Height="300">
            <StackPanel VerticalAlignment="Center"
                        HorizontalAlignment="Center">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Height="24"
                               Width="200"
                               Text="Student Name"></TextBlock>
                    <TextBox  Height="24"
                              Width="200"
                              Text="{Binding Path=Dat.Name, Mode=TwoWay}">
                    </TextBox>
                </StackPanel>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Height="24"
                               Width="200"
                               Text="Student Gender"></TextBlock>
                    <TextBox  Height="24"
                              Width="200"
                              Text="{Binding Path=Dat.Gender, Mode=TwoWay}">
                    </TextBox>
                </StackPanel>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Height="24"
                               Width="200"
                               Text="Student Address"></TextBlock>
                    <TextBox  Height="24"
                              Width="200"
                              Text="{Binding Path=Dat.Address, Mode=TwoWay}">
                    </TextBox>
                </StackPanel>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Height="24"
                               Width="200"
                               Text="Student Score"></TextBlock>
                    <TextBox  Height="24"
                              Width="200"
                              Text="{Binding Path=Dat.Score, Mode=TwoWay}">
                    </TextBox>
                </StackPanel>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Height="24"
                               Width="200"
                               Text="Student Pass Exam If"></TextBlock>
                    <TextBox  Height="24"
                              Width="200"
                              Text="{Binding Path=Dat.PassExamIf, Mode=TwoWay}">
                    </TextBox>
                </StackPanel>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Height="24"
                               Width="200"
                               Text="Student Photo"></TextBlock>
                    <Border BorderThickness="1"
                            BorderBrush="Silver">
                        <Image x:Name="ImgTest"
                               Height="100"
                               Width="200"
                               Source="{Binding Path=Dat.Photo, Mode=TwoWay}"
                               Stretch="Uniform">
                        </Image>
                    </Border>
                </StackPanel>
                <StackPanel>
                    <StackPanel Orientation="Horizontal">
                        <Button x:Name="CmdGetPicture"
                                Content="GetPicture"></Button>
                        <Button x:Name="CmdSavePicture"
                                Content="SavePicture"></Button>
                    </StackPanel>
                </StackPanel>
            </StackPanel>
        </Grid>
    </UserControl>

    1.2) Page.xaml.cs

    using System;
    using System.Collections.Generic;
    using System.Windows;
    using System.Windows.Controls;


    namespace SLTestX {
        public partial class Page : UserControl {
            public Page() {
                InitializeComponent();
                var DtMain = new DT.TFDataTable();

                DtMain.Rows.Add(new DT.TFDataRow());
                DtMain.Rows[0].Fields.Add(new DT.TFDataField("Name", "TFSoft"));
                DtMain.Rows[0].Fields.Add(new DT.TFDataField("Gender", "Male"));
                DtMain.Rows[0].Fields.Add(new DT.TFDataField("BirthDate", DateTime.Now));
                DtMain.Rows[0].Fields.Add(new DT.TFDataField("Address", "Nanchang City, PRC"));
                DtMain.Rows[0].Fields.Add(new DT.TFDataField("Score", 100));
                DtMain.Rows[0].Fields.Add(new DT.TFDataField("PassExamIf", true));
                DtMain.Rows[0].Fields.Add(new DT.TFDataField("Photo", new Byte[] { }));

                DtMain.Rows.Add(new DT.TFDataRow());
                DtMain.Rows[1].Fields.Add(new DT.TFDataField("Name", "Unknown Name"));
                DtMain.Rows[1].Fields.Add(new DT.TFDataField("Gender", "Unknown Gender"));
                DtMain.Rows[1].Fields.Add(new DT.TFDataField("BirthDate", DateTime.Now));
                DtMain.Rows[1].Fields.Add(new DT.TFDataField("Address", "Unknown Address"));
                DtMain.Rows[1].Fields.Add(new DT.TFDataField("Score", 40));
                DtMain.Rows[1].Fields.Add(new DT.TFDataField("PassExamIf", false));
                DtMain.Rows[1].Fields.Add(new DT.TFDataField("Photo", new Byte[] { }));

                GrdRoot.DataContext = new DT.TFBindingSourceOfDataRow(DtMain.Rows[0]);
            }
        }
    }

     



    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Reflection;
    using System.Reflection.Emit;
    using System.Text;
    using System.Windows.Data;

    //------------------------------------------------------------------------
    // Description  : Silverlight
    // Developper   : TFSoft Worrkshop
    // Version      : 1.0
    // Last Updated : 2009/08/27
    // Copyright    : Open Source
    //------------------------------------------------------------------------
    namespace DT {
        //------------------------------------------------------------------------
        //TFDataType
        //------------------------------------------------------------------------
        public class TFDataType {
            public const Int32 TFUnknown = 0; public const Int32 TFText = 1; public const Int32 TFNumber = 2;
            public const Int32 TFDateTime = 3; public const Int32 TFBoolean = 4; public const Int32 TFData = 5;
        };
        //------------------------------------------------------------------------
        //TFDataValueObject
        //------------------------------------------------------------------------
        public class TFDataValueObject {
            private Int32 _DataType; public Int32 DataType { get { return _DataType; } }
            private Object _DataValue; public Object DataValue { get { return _DataValue; } set { _DataValue = value; } }
            public TFDataValueObject() { InitSub(null); }
            public TFDataValueObject(Object TheNetVar) { InitSub(TheNetVar); }
            private void InitSub(Object TheNetVar) {
                _DataType = TFDataType.TFUnknown; _DataValue = null;
                if (TheNetVar != null) {
                    if (TheNetVar.GetType() == typeof(String)) {
                        _DataType = TFDataType.TFText; DataValue = TheNetVar;
                    } else if (TheNetVar.GetType() == typeof(Int16)) {
                        _DataType = TFDataType.TFNumber; DataValue = TheNetVar;
                    } else if (TheNetVar.GetType() == typeof(Int32)) {
                        _DataType = TFDataType.TFNumber; DataValue = TheNetVar;
                    } else if (TheNetVar.GetType() == typeof(Int64)) {
                        _DataType = TFDataType.TFNumber; DataValue = TheNetVar;
                    } else if (TheNetVar.GetType() == typeof(Single)) {
                        _DataType = TFDataType.TFNumber; DataValue = TheNetVar;
                    } else if (TheNetVar.GetType() == typeof(Double)) {
                        _DataType = TFDataType.TFNumber; DataValue = TheNetVar;
                    } else if (TheNetVar.GetType() == typeof(Decimal)) {
                        _DataType = TFDataType.TFNumber; DataValue = TheNetVar;
                    } else if (TheNetVar.GetType() == typeof(DateTime)) {
                        _DataType = TFDataType.TFDateTime; DataValue = TheNetVar;
                    } else if (TheNetVar.GetType() == typeof(Boolean)) {
                        _DataType = TFDataType.TFBoolean; DataValue = TheNetVar;
                    } else if (TheNetVar.GetType() == typeof(Byte[])) {
                        _DataType = TFDataType.TFData; DataValue = TheNetVar;
                    } else {
                        throw new NotSupportedException("DataType [" + DataValue.GetType().ToString() + "] not supported.");
                    }
                }
            }
            public override String ToString() {
                var Rlt = ""; try { Rlt = Convert.ToString(_DataValue); } catch { Rlt = ""; } return Rlt;
            }
            public Double ToDouble() {
                var Rlt = (Double)0; try { Rlt = Convert.ToDouble(_DataValue); } catch { Rlt = (Double)0; }; return Rlt;
            }
            public DateTime ToDateTime() {
                var Rlt = DateTime.MinValue;
                try { Rlt = Convert.ToDateTime(_DataValue); } catch { Rlt = DateTime.MinValue; }; return Rlt;
            }
            public Boolean ToBoolean() {
                var Rlt = false;
                try { Rlt = Convert.ToBoolean(_DataValue); } catch { Rlt = false; }; return Rlt;
            }
            public Byte[] ToBytes() {
                var Rlt = new Byte[] { };
                if (_DataValue != null) {
                    if (_DataValue.GetType().ToString() == typeof(Byte[]).ToString()) { Rlt = (Byte[])_DataValue; }
                }
                return Rlt;
            }
            public Boolean IsNull() {
                return _DataValue == null ? true : false;
            }
        }
        //------------------------------------------------------------------------
        //TFDataField
        //------------------------------------------------------------------------
        public class TFDataField {
            private String _FieldName; private TFDataValueObject _FieldValue;
            public TFDataField() { InitSub("FieldNameX", null); }
            public TFDataField(String TheFieldName) { InitSub(TheFieldName, null); }
            public TFDataField(String TheFieldName, Object TheFieldValue) { InitSub(TheFieldName, TheFieldValue); }
            private void InitSub(String TheFieldName, Object TheFieldValue) {
                _FieldName = TheFieldName; _FieldValue = new TFDataValueObject(TheFieldValue);
            }
            public String FieldName {
                get { return _FieldName; }
                set { if ((value + "").Trim() != "") { _FieldName = value; } }
            }
            public TFDataValueObject FieldValue {
                get { return _FieldValue; }
                set { _FieldValue = new TFDataValueObject(value); }
            }
            public Int32 FieldType {
                get { return _FieldValue.DataType; }
            }
            public String FieldText {
                get { return _FieldValue.ToString(); }
                set { _FieldValue = new TFDataValueObject(value); }
            }
            public Double FieldNumber {
                get { return _FieldValue.ToDouble(); }
                set { _FieldValue = new TFDataValueObject(value); }
            }
            public DateTime FieldDateTime {
                get { return _FieldValue.ToDateTime(); }
                set { _FieldValue = new TFDataValueObject(value); }
            }
            public Boolean FieldBoolean {
                get { return _FieldValue.ToBoolean(); }
                set { _FieldValue = new TFDataValueObject(value); }
            }
            public Byte[] FieldData {
                get { return _FieldValue.ToBytes(); }
                set { _FieldValue = new TFDataValueObject(value); }
            }
            public Boolean FieldIsNull {
                get { return _FieldValue.IsNull(); }
            }
        }
        //------------------------------------------------------------------------
        //TFDataFieldCollection
        //------------------------------------------------------------------------
        public class TFDataFieldCollection {
            private List<TFDataField> _FieldList;
            public TFDataFieldCollection() {
                InitSub();
            }
            private void InitSub() {
                _FieldList = new List<TFDataField>();
            }
            public Int32 Count {
                get { return _FieldList.Count; }
            }
            public TFDataField this[Int32 TheFieldIndex] {
                get { return GetItem(TheFieldIndex); }
            }
            public TFDataField this[String TheFieldName] {
                get { return GetItem(TheFieldName); }
            }
            public Boolean Exsit(TFDataField TheField) {
                return _FieldList.Contains(TheField);
            }
            public Boolean Exsit(Int32 TheFieldIndex) {
                return _FieldList.Contains(GetItem(TheFieldIndex));
            }
            public Boolean Exsit(String TheFieldName) {
                return _FieldList.Contains(GetItem(TheFieldName));
            }
            public void Clear() {
                _FieldList.Clear();
            }
            public Boolean Add(TFDataField TheField) {
                var Rlt = true; try { _FieldList.Add(TheField); } catch { Rlt = false; }; return Rlt;
            }
            public Boolean Insert(Int32 TheFieldIndex, TFDataField TheField) {
                var Rlt = true; try { _FieldList.Insert(TheFieldIndex, TheField); } catch { Rlt = false; }; return Rlt;
            }
            public Boolean Remove(TFDataField TheField) {
                var Rlt = true; try { _FieldList.Remove(TheField); } catch { Rlt = false; }; return Rlt;
            }
            public Boolean RemoveAt(Int32 TheFieldIndex) {
                var Rlt = true; try { _FieldList.RemoveAt(TheFieldIndex); } catch { Rlt = false; }; return Rlt;
            }
            public TFDataField GetItem(Int32 TheFieldIndex) {
                var Rlt = (TFDataField)null; try { Rlt = _FieldList[TheFieldIndex]; } catch { Rlt = null; }; return Rlt;
            }
            public TFDataField GetItem(String TheFieldName) {
                var Rlt = (TFDataField)null; TheFieldName = (TheFieldName.Trim()).ToUpper();
                for (var I = 0; I < _FieldList.Count; I++) {
                    if ((_FieldList[I].FieldName + "").ToUpper() == TheFieldName) { Rlt = _FieldList[I]; break; }
                }
                return Rlt;
            }
        }
        //------------------------------------------------------------------------
        //TFDataRow
        //------------------------------------------------------------------------
        public class TFDataRow {
            private TFDataFieldCollection _FieldCollection;
            public TFDataRow() { InitSub(); }
            private void InitSub() {
                _FieldCollection = new TFDataFieldCollection();
            }
            public TFDataFieldCollection Fields {
                get { return _FieldCollection; }
                set { _FieldCollection = value; }
            }
        }
        //------------------------------------------------------------------------
        //TFDataRowCollection
        //------------------------------------------------------------------------
        public class TFDataRowCollection {
            private List<TFDataRow> _RowList;
            public TFDataRowCollection() {
                InitSub();
            }
            private void InitSub() {
                _RowList = new List<TFDataRow>();
            }
            public Int32 Count {
                get { return _RowList.Count; }
            }
            public TFDataRow this[Int32 TheRowIndex] {
                get { return GetItem(TheRowIndex); }
            }
            public Boolean Exsit(TFDataRow TheRow) {
                return _RowList.Contains(TheRow);
            }
            public Boolean Exsit(Int32 TheRowIndex) {
                return _RowList.Contains(GetItem(TheRowIndex));
            }
            public void Clear() {
                _RowList.Clear();
            }
            public Boolean Add(TFDataRow TheRow) {
                var Rlt = true; try { _RowList.Add(TheRow); } catch { Rlt = false; }; return Rlt;
            }
            public Boolean Insert(Int32 TheRowIndex, TFDataRow TheRow) {
                var Rlt = true; try { _RowList.Insert(TheRowIndex, TheRow); } catch { Rlt = false; }; return Rlt;
            }
            public Boolean Remove(TFDataRow TheRow) {
                var Rlt = true; try { _RowList.Remove(TheRow); } catch { Rlt = false; }; return Rlt;
            }
            public Boolean RemoveAt(Int32 TheRowIndex) {
                var Rlt = true; try { _RowList.RemoveAt(TheRowIndex); } catch { Rlt = false; }; return Rlt;
            }
            public TFDataRow GetItem(Int32 TheRowIndex) {
                var Rlt = (TFDataRow)null; try { Rlt = _RowList[TheRowIndex]; } catch { Rlt = null; }; return Rlt;
            }
        }
        //------------------------------------------------------------------------
        //TFDataTable
        //------------------------------------------------------------------------
        public class TFDataTable {
            private TFDataRowCollection _RowCollection;
            public TFDataTable() { InitSub(); }
            private void InitSub() {
                _RowCollection = new TFDataRowCollection();
            }
            public TFDataRowCollection Rows {
                get { return _RowCollection; }
                set { _RowCollection = value; }
            }
            public TFDataField this[Int32 TheRecNo, Int32 TheFieldNo] {
                get { return GetItem(TheRecNo, TheFieldNo); }
            }
            public TFDataField this[Int32 TheRecNo, String TheFieldName] {
                get { return GetItem(TheRecNo, TheFieldName); }
            }
            public TFDataField GetItem(Int32 TheRecNo, Int32 TheFieldIndex) {
                var Rlt = (TFDataField)null; var Row = _RowCollection.GetItem(TheRecNo);
                if (Row != null) { Rlt = Row.Fields.GetItem(TheFieldIndex); }; return Rlt;
            }
            public TFDataField GetItem(Int32 TheRecNo, String TheFieldName) {
                var Rlt = (TFDataField)null; var Row = _RowCollection.GetItem(TheRecNo);
                if (Row != null) { Rlt = Row.Fields.GetItem(TheFieldName); }; return Rlt;
            }
        }
        //----------------------------------------------------------------------------
        //TFDataSerilizer
        //----------------------------------------------------------------------------
        public abstract class TFDataSerilizer {
            //----------------------------------------------------------------------------
            //TFDataValueSerilizer
            //----------------------------------------------------------------------------
            public class TFDataObjectSerilizer {
                public static String Serilize(TFDataValueObject TheDataObject) {
                    var Txt = "";
                    switch (TheDataObject.DataType) {
                        case TFDataType.TFUnknown:
                            Txt = "";
                            break;
                        case TFDataType.TFText:
                            Txt = Convert.ToBase64String(Encoding.UTF8.GetBytes(TheDataObject.ToString()));
                            break;
                        case TFDataType.TFNumber: Txt = Convert.ToString(TheDataObject.ToDouble()); break;
                        case TFDataType.TFDateTime:
                            Txt = Convert.ToBase64String(Encoding.UTF8.GetBytes(TheDataObject.ToDateTime().ToString("yyyy-MM-dd HH:mm:ss")));
                            break;
                        case TFDataType.TFBoolean:
                            Txt = TheDataObject.ToBoolean().ToString();
                            break;
                        case TFDataType.TFData:
                            Txt = Convert.ToBase64String(TheDataObject.ToBytes());
                            break;
                        default:
                            break;
                    }
                    return TheDataObject.DataType + TFDataSeperator.TypeDeli + Txt;
                }
                public static TFDataValueObject DeSerilize(String TheDataObjectSerStr) {
                    var Rlt = new TFDataValueObject();
                    var I = 0; var Buf = new Byte[] { };
                    var ATxt = TheDataObjectSerStr.Split(new String[] { TFDataSeperator.TypeDeli }, StringSplitOptions.None);
                    if (ATxt.Length > 1) {
                        try { I = Convert.ToInt32(ATxt[0]); } catch { I = 0; };
                        switch (I) {
                            case TFDataType.TFUnknown:
                                Rlt.DataValue = null;
                                break;
                            case TFDataType.TFText:
                                Buf = Convert.FromBase64String(ATxt[1]);
                                Rlt.DataValue = Encoding.UTF8.GetString(Buf, 0, Buf.Length);
                                break;
                            case TFDataType.TFNumber:
                                try { Rlt.DataValue = Convert.ToDouble(ATxt[1]); } catch { };
                                break;
                            case TFDataType.TFDateTime:
                                Buf = Convert.FromBase64String(ATxt[1]);
                                try { Rlt.DataValue = Convert.ToDateTime(Encoding.UTF8.GetString(Buf, 0, Buf.Length)); } catch { }
                                break;
                            case TFDataType.TFBoolean:
                                try { Rlt.DataValue = Convert.ToBoolean(ATxt[1]); } catch { }
                                break;
                            default:
                                break;
                        }
                    }
                    return Rlt;
                }
            }
            //----------------------------------------------------------------------------
            //TFDataFieldSerilizer
            //----------------------------------------------------------------------------
            public class TFDataFieldSerilizer {
                public static String Serilize(TFDataField TheDataField) {
                    var Rlt = new StringBuilder();
                    Rlt.Append(TheDataField.FieldName); Rlt.Append(TFDataSeperator.EqDeli);
                    Rlt.Append(TheDataField.FieldType.ToString()); Rlt.Append(TFDataSeperator.TypeDeli);
                    Rlt.Append(TFDataObjectSerilizer.Serilize(TheDataField.FieldValue));
                    return Rlt.ToString();
                }
                public static TFDataField DeSerilize(String TheDataFieldSerStr) {
                    var Rlt = new TFDataField();
                    var ATxt = TheDataFieldSerStr.Split(new String[] { TFDataSeperator.EqDeli }, StringSplitOptions.None);
                    if (ATxt.Length > 0) { Rlt.FieldName = ATxt[0]; };
                    if (ATxt.Length > 1) { Rlt.FieldValue = TFDataObjectSerilizer.DeSerilize(ATxt[1]); }
                    return Rlt;
                }
            }
            //----------------------------------------------------------------------------
            //TFDataRowSerilizer
            //----------------------------------------------------------------------------
            public class TFDataRowSerilizer {
                public static String Serilize(TFDataRow TheDataRow) {
                    var Rlt = new StringBuilder();
                    for (var I = 0; I < TheDataRow.Fields.Count; I++) {
                        Rlt.Append(TFDataFieldSerilizer.Serilize(TheDataRow.Fields[I]));
                        if (I < TheDataRow.Fields.Count - 1) Rlt.Append(TFDataSeperator.FieldDeli);
                    }
                    return Rlt.ToString();
                }
                public static TFDataRow DeSerilize(String TheDataRowSerStr) {
                    var Rlt = new TFDataRow();
                    var ATxt = TheDataRowSerStr.Split(new String[] { TFDataSeperator.FieldDeli }, StringSplitOptions.None);
                    for (var I = 0; I < ATxt.Length; I++) {
                        Rlt.Fields.Add(TFDataFieldSerilizer.DeSerilize(ATxt[I]));
                    }
                    return Rlt;
                }
            }
            //----------------------------------------------------------------------------
            //TFDataTableSerilizer
            //----------------------------------------------------------------------------
            public class TFDataTableSerilizer {
                public static String Serilize(TFDataTable TheDataTable) {
                    var Rlt = new StringBuilder();
                    for (var I = 0; I < TheDataTable.Rows.Count; I++) {
                        Rlt.Append(TFDataRowSerilizer.Serilize(TheDataTable.Rows[I]));
                        if (I < TheDataTable.Rows.Count - 1) Rlt.Append(TFDataSeperator.RecordDeli);
                    }
                    return Rlt.ToString();
                }
                public static TFDataTable DeSeriliz(String TheDataTableSerStr) {
                    var Rlt = new TFDataTable();
                    var ATxt = TheDataTableSerStr.Split(new String[] { TFDataSeperator.RecordDeli }, StringSplitOptions.None);
                    for (var I = 0; I < ATxt.Length; I++) {
                        Rlt.Rows.Add(TFDataRowSerilizer.DeSerilize(ATxt[I]));
                    }
                    return Rlt;
                }
            }
            //------------------------------------------------------------------------
            //TFDataSeperator
            //------------------------------------------------------------------------
            private class TFDataSeperator {
                public const String EqDeli = ":"; public const String TypeDeli = ",";
                public const String FieldDeli = ";"; public const String RecordDeli = "#"; public const String TableDeli = "¥";
            }
        }
        //------------------------------------------------------------------------
        //TFBindingSourceBase
        //------------------------------------------------------------------------
        public abstract class TFBindingSourceBase : INotifyPropertyChanged {
            private Object DynamicInstanceObject;
            public event PropertyChangedEventHandler PropertyChanged;
            public TFBindingSourceBase() { DynamicInstanceObject = null; }
            private void NotifyPropChange(String PropName) {
                if (this.PropertyChanged != null) {
                    this.PropertyChanged(this, new PropertyChangedEventArgs(PropName));
                }
            }
            public Object Dat {
                get { return DynamicInstanceObject; }
                set { DynamicInstanceObject = value; NotifyPropChange("Dat"); }
            }
            //------------------------------------------------------------------------
            //TFDynamicPropertyCreater
            //------------------------------------------------------------------------
            protected class TFDynamicPropertyCreater {
                private TypeBuilder RfnTypeBuilder;
                private Boolean InstanceCreated; private Type RfnInstanceType; private Object RfnInstanceObject;
                public TFDynamicPropertyCreater() {
                    RfnTypeBuilder = CreateTypeBuilder(Guid.NewGuid().ToString().Replace("_", "X").ToUpper());
                    InstanceCreated = false; RfnInstanceType = null; RfnInstanceObject = null;
                }
                public void AddProperty(String ThePropName, Type ThePropType) {
                    if (InstanceCreated) {
                        var ErrMsg = "";
                        ErrMsg += "can not call [AddProperty] after";
                        ErrMsg += "called [SetPropertyValue/GetPropertyValue,GetDynamicInstanceType/GetDynamicInstanceObject].";
                        throw new NotSupportedException(ErrMsg);
                    } else {
                        CreateProperty(RfnTypeBuilder, ThePropName, ThePropType);
                    }
                }
                public void SetPropertyValue(String ThePropName, Object TheValue) {
                    CreateDynamicInstance(); var RfnProp = RfnInstanceType.GetProperty(ThePropName);
                    RfnProp.SetValue(RfnInstanceObject, TheValue, null);
                }
                public Object GetPropertyValue(String ThePropName) {
                    CreateDynamicInstance(); var RfnProp = RfnInstanceType.GetProperty(ThePropName);
                    return RfnProp.GetValue(RfnInstanceObject, null);
                }
                public Object GetDynamicInstanceType() {
                    CreateDynamicInstance(); return RfnInstanceType;
                }
                public Object GetDynamicInstanceObject() {
                    CreateDynamicInstance(); return RfnInstanceObject;
                }
                private void CreateDynamicInstance() {
                    if (!InstanceCreated) {
                        RfnInstanceType = RfnTypeBuilder.CreateType();
                        RfnInstanceObject = Activator.CreateInstance(RfnInstanceType);
                        InstanceCreated = true;
                    }
                }
                private void CreateProperty(TypeBuilder TheTypBuilder, String ThePropName, Type ThePropType) {
                    var TypBuilder = TheTypBuilder;
                    var FldBuilder = TypBuilder.DefineField("_" + ThePropName, ThePropType, FieldAttributes.Private);
                    var ProBuilder = TypBuilder.DefineProperty(ThePropName, PropertyAttributes.HasDefault, ThePropType, null);
                    var MedGetBulider = TypBuilder.DefineMethod("get_" + ThePropName,
                                                     MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig,
                                                     ThePropType, Type.EmptyTypes);
                    var ILGetGenerator = MedGetBulider.GetILGenerator();
                    ILGetGenerator.Emit(OpCodes.Ldarg_0); ILGetGenerator.Emit(OpCodes.Ldfld, FldBuilder); ILGetGenerator.Emit(OpCodes.Ret);
                    var MedSetBuilder = TypBuilder.DefineMethod("set_" + ThePropName,
                                                      MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig,
                                                      null, new Type[] { ThePropType });
                    var ILSetGenerator = MedSetBuilder.GetILGenerator();
                    ILSetGenerator.Emit(OpCodes.Ldarg_0); ILSetGenerator.Emit(OpCodes.Ldarg_1);
                    ILSetGenerator.Emit(OpCodes.Stfld, FldBuilder); ILSetGenerator.Emit(OpCodes.Ret);
                    ProBuilder.SetGetMethod(MedGetBulider); ProBuilder.SetSetMethod(MedSetBuilder);
                }
                private TypeBuilder CreateTypeBuilder(String AsmNameTail) {
                    var AsmName = new AssemblyName("Assembly" + AsmNameTail);
                    var AsmBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(AsmName, AssemblyBuilderAccess.Run);
                    var ModBuilder = AsmBuilder.DefineDynamicModule("Module" + AsmNameTail);
                    var TypBuilder = ModBuilder.DefineType("Type" + AsmNameTail,
                                                      TypeAttributes.Public | TypeAttributes.Class |
                                                      TypeAttributes.AutoClass | TypeAttributes.AnsiClass |
                                                      TypeAttributes.BeforeFieldInit | TypeAttributes.AutoLayout,
                                                      typeof(Object));
                    return TypBuilder;
                }
            }
        }
        //------------------------------------------------------------------------
        //TFBindingSourceOfDataRow
        //------------------------------------------------------------------------
        public class TFBindingSourceOfDataRow : TFBindingSourceBase {
            private TFDataRow RowObj;
            private TFDynamicPropertyCreater DynamicPropertyCreater;
            public TFBindingSourceOfDataRow(TFDataRow TheRowObj) { InitSub(TheRowObj); }
            private void InitSub(TFDataRow TheRowObj) {
                RowObj = TheRowObj; DynamicPropertyCreater = new TFDynamicPropertyCreater();
                for (var I = 0; I < RowObj.Fields.Count; I++) {
                    DynamicPropertyCreater.AddProperty(RowObj.Fields[I].FieldName,
                                                       RowObj.Fields[I].FieldValue.DataValue.GetType());
                }
                for (var I = 0; I < RowObj.Fields.Count; I++) {
                    DynamicPropertyCreater.SetPropertyValue(RowObj.Fields[I].FieldName,
                                                            RowObj.Fields[I].FieldValue.DataValue);
                }
                Dat = DynamicPropertyCreater.GetDynamicInstanceObject();
            }
            public TFDataRow Rlt {
                get {
                    for (var I = 0; I < RowObj.Fields.Count; I++) {
                        RowObj.Fields[I].FieldValue.DataValue =
                            DynamicPropertyCreater.GetPropertyValue(RowObj.Fields[I].FieldName);
                    }
                    return RowObj;
                }
            }
        }
        //------------------------------------------------------------------------
        //TFBindSourceOfDictionary
        //------------------------------------------------------------------------
        public class TFBindSourceOfDictionary : TFBindingSourceBase {
            private Dictionary<String, Object> DicObj;
            private TFDynamicPropertyCreater DynamicPropertyCreater;
            public TFBindSourceOfDictionary(Dictionary<String, Object> TheDicObj) {
                DicObj = TheDicObj; DynamicPropertyCreater = new TFDynamicPropertyCreater();
                foreach (var Item in DicObj) { DynamicPropertyCreater.AddProperty(Item.Key, Item.Value.GetType()); }
                foreach (var Item in DicObj) { DynamicPropertyCreater.SetPropertyValue(Item.Key, Item.Value); }
                Dat = DynamicPropertyCreater.GetDynamicInstanceObject();
            }
            public Dictionary<String, Object> Rlt {
                get {
                    var Rlt = new Dictionary<String, Object>();
                    foreach (var Item in DicObj) {
                        Rlt.Add(Item.Key, DynamicPropertyCreater.GetPropertyValue(Item.Key));
                    };
                    return Rlt;
                }
            }
        }
        //------------------------------------------------------------------------
    }

     

    2009年8月25日 3:45

全部回复

  • 写写好发布到CodePlex上去吧:)


    如果您觉得对您有帮助,请在“是否有帮助”点“是”;如果你觉得回复很满意,请“标记为已解答”
    2009年8月26日 6:52
    版主