LLBLGen 关于类型转换
關于類型轉換的問題,在MS的MSDN上也有提到過,但是LLBLGen 要有一個現(xiàn)在的DLL才可以在設計狀態(tài)下使用。我們只是自己寫寫一個DLL了,在安裝目錄下:\Program Files (x86)\Solutions Design\LLBLGen Pro v2.6 Demo\TypeConverters,我們只要把我寫自己寫好的DLL放在這里,就可以使用了。
比如一些字段我們?yōu)閚vchar(1),我們存的是Y,N,那你當然是想把它轉成bool型了。只有綁定了這個問題才可以解決checkbox的問題。
代碼也不難,我就直接給出來了
namespace CRD.TypeConverters
{
??? [Description("Converter with as core type System.Boolean, for mapping a field with a .NET type System.Boolean onto a string database field")]
??? public class BooleanStringConverter : TypeConverter
??? {
??????? /// <summary>
??????? /// Initializes a new instance of the <see cref="BooleanStringConverter"/> class.
??????? /// </summary>
??????? public BooleanStringConverter()
??????? {
??????? }
??????? /// <summary>
??????? /// Returns whether this converter can convert an object of the given type to the type of this converter (Boolean).
??????? /// </summary>
??????? /// <param name="context">Ignored</param>
??????? /// <param name="sourceType">A <see cref="T:System.Type"/> that represents the type you want to convert from.</param>
??????? /// <returns>
??????? /// ?<see langword="true "/>if this converter can perform the conversion; otherwise, <see langword="false"/>.
??????? /// </returns>
??????? /// <remarks>Accepted types are: String</remarks>
??????? public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
??????? {
??????????? // any integer type is accepted. No fractional types like float/double.
??????????? switch (sourceType.FullName)
??????????? {
??????????????? case "System.String":
??????????????????? return true;
??????????????? default:
??????????????????? return false;
??????????? }
??????? }
??????? /// <summary>
??????? /// Returns whether this converter can convert the object to the specified type.
??????? /// </summary>
??????? /// <param name="context">Ignored</param>
??????? /// <param name="destinationType">A <see cref="T:System.Type"/> that represents the type you want to convert to.</param>
??????? /// <returns>
??????? /// ?<see langword="true "/>if this converter can perform the conversion; otherwise, <see langword="false"/>.
??????? /// </returns>
??????? /// <remarks>Accepted types are: String. True will be converted to 1, false will be
??????? /// converted to 0.</remarks>
??????? public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
??????? {
??????????? // any integer type is accepted. No fractional types like float/double.
??????????? switch (destinationType.FullName)
??????????? {
??????????????? case "System.String":
??????????????????? return true;
??????????????? default:
??????????????????? return false;
??????????? }
??????? }
??????? /// <summary>
??????? /// Converts the given object to the type of this converter (Boolean).
??????? /// </summary>
??????? /// <param name="context">Ignored</param>
??????? /// <param name="culture">Ignored</param>
??????? /// <param name="value">The <see cref="T:System.Object"/> to convert.</param>
??????? /// <returns>
??????? /// An <see cref="T:System.Object"/> that represents the converted value, which is of type boolean.
??????? /// </returns>
??????? /// <exception cref="T:System.NotSupportedException">The conversion could not be performed.</exception>
??????? public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
??????? {
??????????? bool toReturn = true;
??????????? switch (value.GetType().FullName)
??????????? {
??????????????? case "System.String":
??????????????????? toReturn = ((string)value == "Y");
??????????????????? break;
??????????????? case "System.DBNull":
??????????????????? toReturn = false;
??????????????????? break;
??????????????? default:
??????????????????? throw new NotSupportedException("Conversion from a value of type '" + value.GetType().ToString() + "' to System.Boolean isn't supported");
??????????? }
??????????? return toReturn;
??????? }
??????? /// <summary>
??????? /// Converts the given value object to the specified type
??????? /// </summary>
??????? /// <param name="context">Ignored</param>
??????? /// <param name="culture">Ignored</param>
??????? /// <param name="value">The <see cref="T:System.Object"/> to convert.</param>
??????? /// <param name="destinationType">The <see cref="T:System.Type"/> to convert the <paramref name="value"/> parameter to.</param>
??????? /// <returns>
??????? /// An <see cref="T:System.Object"/> that represents the converted value. The value will be 1 if <paramref name="value"/> is true, otherwise 0
??????? /// </returns>
??????? /// <exception cref="T:System.ArgumentNullException">The <paramref name="destinationType"/> parameter is <see langword="null"/>.</exception>
??????? /// <exception cref="T:System.NotSupportedException">The conversion could not be performed.</exception>
??????? public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
??????? {
??????????? if (value == null)
??????????? {
??????????????? throw new ArgumentNullException("value", "Value can't be null");
??????????? }
??????????? if (!(value is bool))
??????????? {
??????????????? throw new ArgumentException("Value isn't of type boolean", "value");
??????????? }
??????????? if ((bool)value)
??????????? {
??????????????? switch (destinationType.FullName)
??????????????? {
??????????????????? case "System.String":
??????????????????????? return (string)"Y";
??????????????????? case "System.DBNull":
??????????????????????? return (string)"N";
??????????????????? default:
??????????????????????? throw new NotSupportedException("Conversion to a value of type '" + destinationType.ToString() + "' isn't supported");
??????????????? }
??????????? }
??????????? else
??????????? {
??????????????? switch (destinationType.FullName)
??????????????? {
??????????????????? case "System.String":
??????????????????? case "System.DBNull":
??????????????????????? return (string)"N";
??????????????????? default:
??????????????????????? throw new NotSupportedException("Conversion to a value of type '" + destinationType.ToString() + "' isn't supported");
??????????????? }
??????????? }
??????? }
??????? /// <summary>
??????? /// Creates an instance of the Type that this <see cref="T:System.ComponentModel.TypeConverter"/> is associated with (bool)
??????? /// </summary>
??????? /// <param name="context">ignored.</param>
??????? /// <param name="propertyValues">ignored.</param>
??????? /// <returns>
??????? /// An <see cref="T:System.Object"/> of type bool. It always returns 'true' for this converter.
??????? /// </returns>
??????? public override object CreateInstance(ITypeDescriptorContext context, System.Collections.IDictionary propertyValues)
??????? {
??????????? return true;
??????? }
??? }
}
我這里只舉一個的類型寫法,至于其它的我就上傳了。下載
至于它的解釋網(wǎng)上看看就可以了,我就不費話了。
轉載于:https://www.cnblogs.com/heyu5220/archive/2009/12/03/1616126.html
總結
以上是生活随笔為你收集整理的LLBLGen 关于类型转换的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 无ldf文件情况下恢复数据库数据纪实
- 下一篇: ASP.NET在线用户列表精确版——解决