Smartkid's binary life

博客园 联系 订阅 管理
  10 Posts :: 1 Stories :: 22 Comments :: 0 Trackbacks

News

2012年10月5日 #

 下午装了 typescript , 在坑里跌进爬出~~~

 

类型强制转换

在C#中,我们可以使用 (T)someVar 将someVar转换成类型T, 在typescript中,则使用 <T> someVar。 

 

万能的 any 

对于不知道类型的对象,例如由页面中的javascript传入的对象, 可以将其声明成 Any。

也可以将任意的对象强制转换为 any,从而绕过强类型检查。例如我们想为预定义的 window 对象增加一个属性,就可以采用:

 

 

    //Cast to any, so we can add a property to an already defined class, 
    //otherwise the compiler stops on error: The property "x" does not exist on value of type 'Window'
    (<any>window).x = someThing;    

 

 

 

模拟名称空间

可以用 Module 模拟 C# 的 Namespace,例如:

module OuterMost {
  export module Inner {
    export function helloWorld() {
    }
  }
 }

注意最外层的 module 不要加 "export" , 否则生成的 javascript 会在文件末尾引用为定义的 "exports" 对象,从而在 javascript 运行过程中报 "Uncaught Reference Error: exports is not defined." 错误

 

模拟私有变量

Javascript 中,对象是没有私有成员的,在 typescript 中同样如此。但和在 javascript 中一样,可以通过在构造函数中定义成员的方式进行模拟。 

 module OuterMost {


    //Cast to any, so we can add property to an already defined class, 
    //otherwise we will get the compiler error: The property "x" does not exist on value of type 'Window'
    (<any>window).x = OuterMost;    //define a short name of the module
     
    export module Inner {

        export function ThisIsStaticFunction() {
            return "Can be called as OuterMost.Inner.ThisIsStaticFunction";
        }

        export class HelloWorld {

            //Public method, note that function is also an object in javascript 
            public GetMessage: () => string;

            constructor (message: string) {
                //private member
                var m_Message: string; 
                
                //Private method 
                var processMessage = function () {
                    return "Message processed for: " + m_Message;
                };

                //Creates the public method
                this.GetMessage = function () {
                    return processMessage();
                };
            }

            //Public method, cannot access the private members.
            public GetMoreMessage(times: number): string[] {
                //Cannot access the private members as they are realy local variables inside the constructor
                //var x = m_Message; 

                var result: string[] = new Array();
                for (var i = 0; i < times; i++) {
                    result[i] = this.GetMessage();  //public members can be accessed.
                }
                return result;
            }
        }
    }
}


希望可以帮到调入同样坑里的~~~ 

posted @ 2012-10-05 22:05 Smartkid 阅读(282) 评论(1) 编辑

作为一个 Javascript 菜鸟,今天下载了 Typescript  尝尝鲜~~不得不说,偶被戳到G点了~~~


Typescript 的优点:

1)终于基本摆脱了 javascript 智能感知和重构的劣势了,好歹现在有个能用的 Rename 了。VS 的 javascript 智能感知就是个捣乱的~

2)作为 javascript 的超集,和 javascript 的兼容性最好,能发挥所有 javascript 的功能,而其他的方案(如 CoffeeScript, Script#) 都或多或少地丢失了部分 javascript 的功能

3)现有的javascript程序不用任何修改就可以成为Typescript 程序,这对现有js代码100%的兼容性使得我们可以递进地重构现有代码

4)作为强类型的面向对象语言,其语法感觉上就像是个简化版的 Object Pascal (还有童鞋记得Delphi吗?),对于C#程序员有着天然的亲和感,基本上拿过 Language Specification 瞄一眼就能干活了

5)生成的 javascript 和 typescript 代码的可对比性非常好,加上 WebEssentials ,对照着生成的 javascript 写 typescript 

6)和 Visual Studio 的集成简单,安装包都帮你搞定了 

恩~~~总之~~~从此偶不害怕写 javascript 了~~~

 


下面提供一些有用的工具/链接:

 

1) http://www.typescriptlang.org/, 上有安装包,还有一个 playground ,可以在浏览器里面立刻体会 typescript。

1) Typescript 源码, 里面有几个特别要提一下的咚咚:

A) jQuery (现在还有做界面不和$打交道的不?) 的typescript定义文件,以得到针对jQuery智能感知和强类型

B)Language Specification,详细介绍了 typescript 的语法

2) WebEssentials  , 一个 Visual Studio 插件,可以在编写 typescript 的同时就能够看到生成的 javascript 代码,不但能学typescript,还可以用它学javascript,嘿嘿~~

3) 用 Source Maps 在Chrome中直接调适 typescript

4) Anders Hejlsberg 关于 typescript 的视频

5) 如果想八卦一下,请看这里啦~~~看了才知道~~除了Hejlsberg这样的大牛,还有 Erich Gamma 都伸了一腿啊~~~ 

 

提示:

针对有人问到是否可以不用 javascript 了,我的看法是:

1)从书写的角度,可以基本不写 javascript 了

2) Javascript 的知识还是必须的,毕竟 typescript 是用来生成 javascript 的,如果对javascript 不了解,那 typescript 也就很难用好了

3) typescript 只能生成单独的 .js 文件,对于嵌在 html / php / aspx / cshtml 里面的 javascript 是不能用 typescript 的,解决方案就是尽量将 javascript 移至单独的文件中,再用 typescript 替换它

posted @ 2012-10-05 18:12 Smartkid 阅读(3766) 评论(14) 编辑

2012年9月20日 #

 
使 Visual Studio 2012 采用大小写混合的主菜单

 在注册表 HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\11.0\General\

下添加 SuppressUppercaseConversion 项,类型为 DWORD,值为1,然后重启 VS

 

采用 Visual Studio 2010 风格的界面色调 

如果觉得 VS 2012 默认的界面颜色不容易很难识别不同的窗口,可以通过菜单项 "Tools/Extensions and Updates" 下载插件 Visual Studio 2012 Color Theme Editor , 然后选择Blue风格。

posted @ 2012-09-20 20:58 Smartkid 阅读(175) 评论(0) 编辑

2012年6月6日 #

Controller.UpdateModel<TModel>(TModel model, ...) 函数族只绑定定义在 TModel 及其基类中定义的属性,而不绑定 TModel 的派生类中定义的属性。

例如: 

    public class MyBase

    {
        public String Foo { getset; }
    }

    public class MyDerived : MyBase
    {
        public String Bar { getset; }
    }

    public class MyController : Controller
    {
        public void Search(FormCollection values)
        {
            MyBase obj = new MyDerived(); //有时我们会根据values的值来决定创建哪个类型的实例
            UpdateModel(obj, values);
            Assert.AreEqual("foo", obj.Foo);
            Assert.AreNotEqual("bar", ((MyDerived)obj).Bar);
        }
    }

    [TestFixture]
    public class UpdateModelTests
    {
        [Test]
        public void Test()
        {
            FormCollection values = new FormCollection();
            values.Add("Foo""foo");
            values.Add("Bar""bar");

            MyController c = new MyController();
            c.ControllerContext = new ControllerContext();
            c.Search(values);
        }
    }


解决方案:


自己构建一个 TryUpdateModel(Object model, ...) 函数,代码可以用 reflector 或者 dotPeek 或者 reSharper 从MVC的类库中复制过来, 并更改

 

                ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => model, typeof(TModel)),  

 

 


                ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => model, model.GetType()), 

 

 

posted @ 2012-06-06 22:51 Smartkid 阅读(709) 评论(0) 编辑

2011年4月10日 #

EntityDataModel 默认生成的实体类派生自 EntityObject:

public class MyUser : EntityObject 

如果要定一个项目通用的基类,如:

public class MyUser : MyEntityObjectBase 


可以修改 tt 文件如下 (diff格式):

 

@@ -315,7 +315,7 @@
 
<#
         }
 #
>
-<#=Accessibility.ForType(entity)#> <#=code.SpaceAfter(code.AbstractOption(entity))#>partial class <#=code.Escape(entity)#> : <#=BaseTypeName(entity, code)#>
+<#=Accessibility.ForType(entity)#> <#=code.SpaceAfter(code.AbstractOption(entity))#>partial class <#=code.Escape(entity)#> <#=BaseTypeName(entity, code)#>
 {
 
<#
         
if (!entity.Abstract)
@@ 
-1022,7 +1022,7 @@
 
 
string BaseTypeName(EntityType entity, CodeGenerationTools code)
 {
-    return entity.BaseType == null ? "EntityObject" : MultiSchemaEscape((StructuralType)entity.BaseType, code);
+    return entity.BaseType == null ? String.Empty : "" + MultiSchemaEscape((StructuralType)entity.BaseType, code);
 }
 

 

这样就禁止了T4模板在生成代码中包含基类,然后手工在partial 类中定义基类。

posted @ 2011-04-10 22:51 Smartkid 阅读(385) 评论(0) 编辑

2008年6月29日 #

The unit test in Visual Studio has a bug:
AppDomain.CurrentDomain.BaseDirectory always returns the installtion directory of the visual studio 2008 instead of the 'Out' directory.

So if your code have to access data files deployed to the 'Out' directory using relative path, error occurs.

A workaround is:

        public static GetApplicationBaseDirectory {
            
if (AppDomain.CurrentDomain.FriendlyName.StartsWith("UnitTestAdapterDomain_")) {
                
return Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            }

                
return AppDomain.CurrentDomain.BaseDirectory;
        }


posted @ 2008-06-29 02:53 Smartkid 阅读(529) 评论(0) 编辑

2007年6月6日 #

posted @ 2007-06-06 14:07 Smartkid 阅读(351) 评论(0) 编辑

2007年5月13日 #

posted @ 2007-05-13 14:52 Smartkid 阅读(895) 评论(0) 编辑

posted @ 2007-05-13 00:33 Smartkid 阅读(573) 评论(0) 编辑

2007年2月22日 #

posted @ 2007-02-22 19:35 Smartkid 阅读(4127) 评论(7) 编辑

仅列出标题