         encoding should have name property, encode and decode functions.
       '
     encoding
+  ###
   constructor: (aTypeName, aOptions)->
+    # create a new instance object if aOptions is not the original options of the type.
+    if not (this instanceof Type) and not (aTypeName instanceof Type)
+      if aTypeName
+        if isObject aTypeName
+          aOptions = aTypeName
+          aTypeName = aOptions.name
+        else if not isString aTypeName
+          aOptions = aTypeName
+          aTypeName = undefined
+      if not aTypeName
+        # arguments.callee is forbidden if strict mode enabled.
+        try vCaller = arguments.callee.caller
+        if vCaller and isInheritedFrom vCaller, Type
+          aTypeName = vCaller
+          vCaller = vCaller.caller
+          #get farest hierarchical registered class
+          while isInheritedFrom vCaller, aTypeName
+            aTypeName = vCaller
+            vCaller = vCaller.caller
+          aTypeName = Type.getNameFromClass(aTypeName) if aTypeName
+        return unless aTypeName
+      vTypeClass = Type.registeredClass aTypeName
+      if vTypeClass and aOptions and not vTypeClass::$attributes.isOriginal(aOptions)
+        return createObject vTypeClass, aOptions
     return super
   initialize: (aOptions)->
     defineProperty @, 'errors', null
+    @$attributes.initializeTo @ if @$attributes
     @_initialize aOptions if @_initialize
     @assign(aOptions)
   finalize: (aOptions)->
     @errors = null if @errors
-    @encoding = null if @encoding
+    #@encoding = null if @encoding
     @_finalize(aOptions) if @_finalize
   assign: (aOptions)->
     @errors = []
     if aOptions
-      @encoding = @getEncoding aOptions.encoding
+      #@encoding = @getEncoding aOptions.encoding
       # assign aOptions to @
       if @$attributes
         @$attributes.assignTo(aOptions, @)

  toJson: (aOptions)->
    result = @toJSON(aOptions)
    JSON.stringify result


module.exports = class Type

    aOptions.name = @name unless aOptions.name
    @createType aOptions
  clone: @::cloneType
  # Get(create) a global Type class or create new Value from the json string.
  # it will create a new type object if options is not the original type options.
  @fromJson: (aString)->
    #aString = JSON.parse aString
    Type.from JSON.parse(aString)
