今天工作中发现,在VS2010里面用Dynamic Data自动生成出来的表格中,Text类型的列竟然只显示25个字符?,如下图:
其中超过25字符的部分都被”…”略去了, Google无答案,简单Code Review一下应该是Code模板的问题,可以如下修改:
1. 打开文件~\DynamicData\FieldTemplates\Text.ascx.cs
2. 修改其中变量MAX_DISPLAYLENGTH_IN_LIST为你想要的值
public partial class TextField : System.Web.DynamicData.FieldTemplateUserControl
{
private const int MAX_DISPLAYLENGTH_IN_LIST = 50;
public override string FieldValueString
{
get
{
string value = base.FieldValueString;
if (ContainerType == ContainerType.List)
{
if (value != null && value.Length > MAX_DISPLAYLENGTH_IN_LIST)
{
value = value.Substring(0, MAX_DISPLAYLENGTH_IN_LIST - 3) + "...";
}
}
return value;
}
}
public override Control DataControl
{
get
{
return Literal1;
}
}
}
好了,大功告成。
在与复杂数据操作有关的项目中,我们常常需要将种种复杂的源数据归类到一个通用的数据表格上,再传给之后的程序继续处理。
.Net Framework 引入LINQ之后,我们基本上可以依赖LINQ对各类不同格式、不同来源的数据使用统一的查询语句来进行“格式化”,获取这些数据源中的数据并转换到我们想要的格式上来。
但是我们一直受到一个问题的困扰,那就是用来查询数据的LINQ语句本身是在编译前就确定下来的,这使得我们使用LINQ语句的扩展性大大降低。每当我们要使用一种新的数据源或者修改对一种数据源的查询逻辑时,我们必须更新代码并重新发布软件。
人们经常将LINQ和T/SQL查询语句做对比。T/SQL中有EXECUTE语句可以解析并执行Runtime生成的查询字符串。更有甚者,还有sp_executesql,甚至可以执行带参数的T/SQL语句块,并传入参数。如下例:
DECLARE @IntVariable int;
DECLARE @SQLString nvarchar(500);
DECLARE @ParmDefinition nvarchar(500);
DECLARE @max_title varchar(30);
SET @IntVariable = 197;
SET @SQLString = N'SELECT @max_titleOUT = max(Title)
FROM AdventureWorks.HumanResources.Employee
WHERE ManagerID = @level';
SET @ParmDefinition = N'@level tinyint, @max_titleOUT varchar(30) OUTPUT';
EXECUTE sp_executesql @SQLString,
@ParmDefinition,
@level = @IntVariable,
@max_titleOUT=@max_title OUTPUT;
SELECT @max_title;
通过给出@SQLString和@ParmDefinition,事实上定义了一个存储过程。再给出传入方法的参数,就可以简单的执行这个存储过程,并获取其执行结果。由此,从函数定义,函数内的逻辑到函数的参数值,都可以在Runtime给出。而其输出值,即执行结果也能方便的得到。
那么,我们能不能这样使用LINQ呢?从Scott的blog和MSDN的LINQSample我们可以看到微软官方也是有这方面的考虑的。只是最后仍然没有发布这样一个方法,可能是因为就像SQL Server一样,这种方式带来的安全性隐患层出不穷。
不过呢,基于现有的.Net Framework,我们不妨来山寨一下这样一个方法:
namespace System.Linq.Dynamic
{
public static class IEnumerableExtender
{
public static IEnumerable<object> ExecuteLinq<T>(
this IEnumerable<T> enumerableObject,
string linqString,
string parmDefinition,
params object[] parameters)
{
}
}
}
MSDN的namespace System.Linq.Dynamic是通过分析“where string”和“select string”,并借助表达式树来完成c的,虽然安全得多,但未免限制重重。我决定从其他方向入手。很快就发现了这篇CodeDom extensions and dynamic LINQ (string/script to LINQ emitting)。CodeDom,确实是一个很诱人的思路。
OK,第一步,把Igor的CodeDom extensions简单的整理一下,没有花精力去补全属性、变量,委托和内部类等相关的extensions,只有方法的,也就整了下面这个CodeDomHelper:
// $File: CodeDomHelper.cs $
// This class provides for extending the CodeDom library
// to help coding CodeDom more gracefully with .Net 3.5.
// History:
// $Author: noahwei $
// $Editor: noahwei $
// $Reviwer: noahwei $
// $LastModifyDate: [12/17/2008] $
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Linq;
using System.Text;
namespace System
{
/// <summary>
/// Extensions Method
/// </summary>
public static class ReflectionExtensions
{
/// <summary>
/// An simply way to invode an dynamic genereted method
/// </summary>
/// <param name="methodInfo">The method you are call</param>
/// <param name="parameters">The parameters which should be pass to the method</param>
/// <returns></returns>
public static object Invoke(this MethodInfo methodInfo, params object[] parameters)
{
return methodInfo.Invoke(null, parameters);
}
}
}
namespace System.CodeDom.Extender
{
/// <summary>
/// CodeDom Helper
/// </summary>
public class CodeDom
{
private List<CodeCompileUnit> listCompileUnits = new List<CodeCompileUnit>();
private List<CodeNamespace> listNamespaces = new List<CodeNamespace>();
private System.Collections.Specialized.StringCollection listReferencedAssemblies =
new System.Collections.Specialized.StringCollection() { "mscorlib.dll" };
/// <summary>
/// Code language. You can choose C# or VB.
/// </summary>
public enum Language
{
CSharp,
VB
}
private Language _language;
/// <summary>
/// Create an CodeDom class for C# code
/// I didn't expose the VB CodeDom now since I don't knwon it well.
/// </summary>
public CodeDom() : this(Language.CSharp) { }
private CodeDom(Language language)
{
_language = language;
}
/// <summary>
/// This is a static methos which can help you to get
/// an inctence of the System.CodeDom.Compiler.CodeDomProvider
/// that is ready for .Net v3.5 code.
/// </summary>
/// <param name="provider">Chosse your coding language</param>
/// <returns></returns>
public static CodeDomProvider Provider(Language provider)
{
var providerOptions =
new Dictionary<string, string>(); providerOptions.Add("CompilerVersion", "v3.5");
switch (provider)
{
case Language.VB:
return new Microsoft.VisualBasic.VBCodeProvider(providerOptions);
case Language.CSharp:
default:
return new Microsoft.CSharp.CSharpCodeProvider(providerOptions);
}
}
/// <summary>
/// Same as the following code snippet,
///
/// namespace namespaceName
/// {
/// ...
/// }
/// </summary>
/// <param name="namespaceName">The name of this namespace</param>
/// <returns></returns>
public CodeNamespace AddNamespace(string namespaceName)
{
CodeNamespace codeNamespace = new CodeNamespace(namespaceName);
listNamespaces.Add(codeNamespace);
return codeNamespace;
}
/// <summary>
/// Same as adding an reference assembly
/// </summary>
/// <param name="referencedAssembly">The reference assembly name. Such as "System.dll"</param>
/// <returns></returns>
public CodeDom AddReference(string referencedAssembly)
{
listReferencedAssemblies.Add(referencedAssembly);
return this;
}
/// <summary>
/// A helper method for creating an Class by CodeDom.
/// Same as the following code snippet,
///
/// public class ClassName
/// {
/// ...
/// }
/// </summary>
/// <param name="className">The name of the Class you are creating.</param>
/// <returns></returns>
public CodeTypeDeclaration Class(string className)
{
return new CodeTypeDeclaration(className);
}
/// <summary>
/// A helper method for creating an Method by CodeDom.
/// Same as the following code snippet,
///
/// public static returnType methodName(paramList)
/// {
/// methodBody
/// }
/// </summary>
/// <param name="returnType">The return type defination</param>
/// <param name="methodName">The method name</param>
/// <param name="paramList">The parameter list</param>
/// <param name="methodBody">The literal code fragment for the method body</param>
/// <returns></returns>
public CodeSnippetTypeMember Method(
string returnType,
string methodName,
string paramList,
string methodBody)
{
return Member(
string.Format("public static {0} {1}({2}) {{ {3} }} ",
returnType,
methodName,
paramList,
methodBody));
}
/// <summary>
/// A helper method for creating an Method by CodeDom.
/// Same as the following code snippet,
///
/// public static void methodName(paramList)
/// {
/// methodBody
/// }
/// </summary>
/// <param name="methodName">The method name</param>
/// <param name="paramList">The parameter list</param>
/// <param name="methodBody">The literal code for the method body</param>
/// <returns></returns>
public CodeSnippetTypeMember Method(
string methodName,
string paramList,
string methodBody)
{
return Method("void", methodName, paramList, methodBody);
}
/// <summary>
/// A helper method for creating an Method by CodeDom.
/// Same as the following code snippet,
///
/// public static void methodName()
/// {
/// methodBody
/// }
/// </summary>
/// <param name="methodName">The method name</param>
/// <param name="methodBody">The literal code fragment for the method body</param>
/// <returns></returns>
public CodeSnippetTypeMember Method(string methodName, string methodBody)
{
return Method("void", methodName, "", methodBody);
}
/// <summary>
/// A helper method for creating an Method by CodeDom.
/// Same as construction of class CodeSnippetTypeMember
/// </summary>
/// <param name="methodBody">The literal code fragment for the type member</param>
/// <returns></returns>
public CodeSnippetTypeMember Member(string memberBody)
{
return new CodeSnippetTypeMember(memberBody);
}
/// <summary>
/// A new CodeCompileUnit to contain the program graph.
/// </summary>
public CodeCompileUnit CompileUnit
{
get
{
// Create a new CodeCompileUnit to contain
// the program graph.
CodeCompileUnit compileUnit = new CodeCompileUnit();
foreach (var ns in listNamespaces)
compileUnit.Namespaces.Add(ns);
return compileUnit;
}
}
/// <summary>
/// Compile the code we have now to an assembly.
/// </summary>
/// <returns></returns>
public Assembly Compile()
{
return Compile(null);
}
/// <summary>
/// Compile the code we have now to an assembly.
/// </summary>
/// <param name="assemblyPath">The path where the assembly file drop to</param>
/// <returns></returns>
public Assembly Compile(string assemblyPath)
{
CompilerParameters options = new CompilerParameters();
options.IncludeDebugInformation = false;
options.GenerateExecutable = false;
options.GenerateInMemory = (assemblyPath == null);
foreach (string refAsm in listReferencedAssemblies)
options.ReferencedAssemblies.Add(refAsm);
//Add assembly which is referanced by current assembly also.
List<string[]> refAsmRefs = new List<string[]>();
try
{
refAsmRefs.AddRange(
from a in Assembly.GetEntryAssembly().GetReferencedAssemblies()
select new string[] { a.Name, a.FullName });
refAsmRefs.AddRange(
from a in Assembly.GetCallingAssembly().GetReferencedAssemblies()
select new string[] { a.Name, a.FullName });
refAsmRefs.AddRange(
from a in Assembly.GetExecutingAssembly().GetReferencedAssemblies()
select new string[] { a.Name, a.FullName });
}
catch { }
foreach (var refAsmRef in refAsmRefs)
{
string asmNameSimp = refAsmRef[0];
string asmFullName = refAsmRef[1];
if (!(options.ReferencedAssemblies.Contains(asmFullName)
|| options.ReferencedAssemblies.Contains(asmNameSimp)))
{
options.ReferencedAssemblies.Add(asmFullName);
}
}
if (assemblyPath != null)
options.OutputAssembly = assemblyPath.Replace('\\', '/');
CodeDomProvider codeProvider = Provider(_language);
CompilerResults results =
codeProvider.CompileAssemblyFromDom(options, CompileUnit);
codeProvider.Dispose();
if (results.Errors.Count == 0)
return results.CompiledAssembly;
// Process compilation errors
System.Diagnostics.Trace.WriteLine("Compilation Errors:");
foreach (string outpt in results.Output)
System.Diagnostics.Trace.WriteLine(outpt);
foreach (CompilerError err in results.Errors)
System.Diagnostics.Trace.WriteLine(err.ToString());
return null;
}
/// <summary>
/// Generate all code to a string.
/// </summary>
/// <returns></returns>
public string GenerateCode()
{
StringBuilder sb = new StringBuilder();
TextWriter tw = new IndentedTextWriter(new StringWriter(sb));
CodeDomProvider codeProvider = Provider(_language);
codeProvider.GenerateCodeFromCompileUnit(
CompileUnit,
tw,
new CodeGeneratorOptions());
codeProvider.Dispose();
tw.Close();
return sb.ToString();
}
}
/// <summary>
/// Converts from "object" general type to a concrete type
/// </summary>
/// <remarks>
/// The class is working with .Net 2.0 and above
/// </remarks>
public class Converter
{
/// <summary>
/// Returns True if the type can get Null as a value
/// (is a reference type and not a value one)
/// </summary>
public static bool IsNullable(Type t)
{
if (!t.IsGenericType) return false;
Type g = t.GetGenericTypeDefinition();
return (g.Equals(typeof(Nullable<>)));
}
/// <summary>
/// Returns a real type of a first generic argument
/// </summary>
private static Type UnderlyingTypeOf(Type t)
{
return t.GetGenericArguments()[0];
}
/// <summary>
/// Converter
/// </summary>
public static T To<T>(object value, T defaultValue)
{
if (value == DBNull.Value) return defaultValue;
Type t = typeof(T);
if (IsNullable(t))
{
if (value == null) return default(T);
t = UnderlyingTypeOf(t);
}
else
{
if ((value == null) && (t.IsValueType))
{
return defaultValue;
}
}
return (T)Convert.ChangeType(value, t);
}
}
/// <summary>
/// The extensions class for the CodeDom Helper
/// </summary>
public static class CodeDomeExtensions
{
/// <summary>
/// Add a class into this namaspace
/// </summary>
/// <param name="codeType">Type Declaration</param>
/// <returns></returns>
public static CodeNamespace AddClass(
this CodeNamespace codeNamespace,
CodeTypeDeclaration codeType)
{
codeNamespace.Types.Add(codeType);
return codeNamespace;
}
/// <summary>
/// Add a member into this class
/// </summary>
/// <param name="memberBody"></param>
/// <returns></returns>
public static CodeTypeDeclaration AddMember(
this CodeTypeDeclaration classCode,
CodeSnippetTypeMember memberBody)
{
classCode.Members.Add(memberBody);
return classCode;
}
/// <summary>
/// Add an imported namespace
/// </summary>
/// <param name="namespaceName"></param>
/// <returns></returns>
public static CodeNamespace Imports(
this CodeNamespace codeNamespace,
string namespaceName)
{
codeNamespace.Imports.Add(
new CodeNamespaceImport(namespaceName));
return codeNamespace;
}
}
}
有了它,一个简单的ExecuteLinq函数唾手可得:
// $File: DynamicLinq.cs $
// This class provides for extending the
// LINQ library to support Dynamic LINQ.
// History:
// $Author: noahwei $
// $Editor: noahwei $
// $Reviwer: noahwei $
// $LastModifyDate: [12/19/2008] $
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.CodeDom.Extender;
namespace System.Linq.Dynamic
{
public static class IEnumerableExtender
{
public static IEnumerable<object> ExecuteLinq<T>(
this IEnumerable<T> enumerableObject,
string linqString,
string parmDefinition,
params object[] parameters)
{
var c = new System.CodeDom.Extender.CodeDom();
c.AddReference("mscorlib.dll")
.AddReference("System.Core.dll")
.AddReference("System.Data.dll")
.AddReference("System.Data.Linq.dll")
.AddReference("System.Xml.dll")
.AddReference("System.Xml.Linq.dll")
.AddNamespace("DynamicLinq")
.Imports("System.Collections.Generic")
.Imports("System.Linq")
.Imports("System.Data.Linq")
.Imports("System.Xml.Linq")
.AddClass(c.Class("DynamicLinqHelper")
.AddMember(c.Method("IEnumerable<object>",
"ExecuteLinq",
parmDefinition,
linqString)));
var method = c.Compile()
.GetType("DynamicLinq.DynamicLinqHelper")
.GetMethod("ExecuteLinq");
return method.Invoke(parameters) as IEnumerable<object>;
}
}
}
OK,够长了,测试分析留待下回。
Love until it hurts
Mother Teresa said:“I have found the paradox that if I love until it hurts,
then there is no hurt, but only more love.”
People are often unreasonable, illogical and self-centered;
Forgive them anyway.
If you are kind, people may accuse you of selfish, ulterior motives;
Be kind anyway.
If you are successful, you will win some false friends
And some true enemies;
Succeed anyway.
If you are honest and frank, people may cheat you;
Be honest and frank anyway.
What you spend years building,
Someone could destroy overnight;
Build anyway.
If you find serenity and happiness, they may be jealous;
Be happy anyway.
The good you do today, people will often forget tomorrow;
Be good anyway.
Give the world the best you have,
And it may never be enough;
Give the world the best you have anyway.
You see, in the final analysis, it is between you and God;
It is never between you and them anyway.
——Mother Teresa(1910-1997)
爱直至成伤
假如你爱至成伤,你会发现,伤没有了,却有更多的爱。
人们经常是不讲道理的、没有逻辑的和以自我为中心的
不管怎样,你要原谅他们
即使你是友善的,人们可能还是会说你自私和动机不良
不管怎样,你还是要友善
当你功成名就,你会有一些虚假的朋友
和一些真实的敌人
不管怎样,你还是要取得成功
即使你是诚实的和率直的,人们可能还是会欺骗你
不管怎样,你还是要诚实和率直
你多年来营造的东西
有人在一夜之间把它摧毁
不管怎样,你还是要去营造
如果你找到了平静和幸福,他人可能会嫉妒你
不管怎样,你还是要去追求幸福
你今天做的善事,人们往往明天就会忘记
不管怎样,你还是要做善事
即使把你最美好的东西给了这个世界
即使这些东西再多都还是不够
不管怎样,你还是要把你最好的东西给这个世界
你看,说到底,这是你和上帝之间的事
而决不是你和他人之间的事
——德蕾莎修女(1910-1997)
所以,人们相信上帝,是因为除了他人以外,人们需要相信还有另外一个存在,使人们的付出变得有意义。
否则,便不会有人去付出,因为付出爱,终成伤害。于是乎,便不会有人去爱。
可是,如果没有人去爱,则没有人会被爱。这世界便没有爱。
所以你相信这个世界有上帝么?或者说,你相信这个世界有爱么?
认真的生活
淡淡的思考
就像这蔚蓝的地球上附着的一个渺小而顽固的尘埃般对抗这令人疯狂而迷惘的命运
流淌
今天
我又加班了
估计
又会到12点
昨天也是
真累
我一直自恃是一个不容易丧失理智的人。但是我有时候不得不怀疑这一点。
金先生说过,练外家功夫的人都是有罩门的,打别得地方都不会破功,可是一被人找到了罩门。那一身横练的功夫都白练了。
我现在觉得自己的罩门被别人拿在了手里,还反复的捏。我每每都在丧失理智的边缘徘徊。
我google了一下,发现像我这样的情况不多,往往都是男友老是和前任会纠缠不清,女孩看上去都是忠诚的。可是,我还是要提醒天下的男人,千万不要和前任女友纠缠不清,这种做法给你身边的人带来的痛苦是无穷尽的。
因为人世间最痛苦的不是谩骂与折磨,而是这种甜蜜与痛苦的夹杂,天堂与地狱的距离,常常相隔只有几分钟。
我忍耐着。
用我已经所剩无多的理智。
来自于岁月的匆匆,我们都活的压力重重。不过这个夏季的日子,还是过的出奇的迷茫。
日子就是迷茫。岁月也是。爱也是。
盲目而迷茫。
走下车的时候是商店都关门的时候了,走进一家日本料理店,点了一桌菜,强迫自己全部都吃掉。
胃的感觉传达不到心脏
因为后者掌管的是记忆
痛
痛的我想钻到你心里
问问他你过往的谜
可是眼泪在提醒我
呕吐时别想别的
专心的吐
有泪水也给我一起吐出去
轻轻的紧握你的手
不放开一丝温柔
汲取你笑着的忧愁
酿一杯断肠的酒
醉里度千秋
凌晨我睡下,辗转不能眠,打个电话去吵醒S,然后便更难于入睡了。翻来覆去的都是一个画面。
侧上的光是个环
在远处旋转
昏暗刺眼的蕴散
扎进撑着发圈的手背那半
你的肩侧过半边
耸起半边
沉下半边
慵懒的脸
边侧垂下间着焦急的青丝一掩

Categories
Tag Cloud
Blog RSS
Comments RSS
Last 50 Posts
Back
Void « Default
Life
Earth
Wind
Water
Fire
Light 