public static void Encode( Packer packer, Type type )
		{
			var assemblyName =
#if !SILVERLIGHT
				type.GetAssembly().GetName();
#else
				new AssemblyName( type.GetAssembly().FullName );
#endif // !SILVERLIGHT

			// Omit namespace prefix when it equals to declaring assembly simple name.
#if DEBUG && !UNITY
			Contract.Assert( type.Namespace != null, "type.Namespace != null" );
#endif // DEBUG && !UNITY

			var compressedTypeName =
				type.Namespace.StartsWith( assemblyName.Name, StringComparison.Ordinal )
				? Elipsis + type.FullName.Substring( assemblyName.Name.Length )
				: type.FullName;
			var version = new byte[ 16 ];
			Buffer.BlockCopy( BitConverter.GetBytes( assemblyName.Version.Major ), 0, version, 0, 4 );
			Buffer.BlockCopy( BitConverter.GetBytes( assemblyName.Version.Minor ), 0, version, 4, 4 );
			Buffer.BlockCopy( BitConverter.GetBytes( assemblyName.Version.Build ), 0, version, 8, 4 );
			Buffer.BlockCopy( BitConverter.GetBytes( assemblyName.Version.Revision ), 0, version, 12, 4 );

			packer.PackArrayHeader( 5 )
				.Pack( compressedTypeName )
				.Pack( assemblyName.Name )
				.Pack( version )
#if !XAMIOS && !XAMDROID
				.Pack( assemblyName.GetCultureName() )
#else
				.Pack( assemblyName.GetCultureName() == "neutral" ? null : assemblyName.GetCultureName() )
#endif // !XAMIOS && !XAMDROID
				.Pack( assemblyName.GetPublicKeyToken() );
		}
Example #2
0
		public static IList<Type> GetTupleItemTypes( Type tupleType )
		{
			Contract.Assert( tupleType.Name.StartsWith( "Tuple`" ) && tupleType.GetAssembly().Equals( typeof( Tuple ).GetAssembly() ) );
			var arguments = tupleType.GetGenericArguments();
			List<Type> itemTypes = new List<Type>( tupleType.GetGenericArguments().Length );
			GetTupleItemTypes( arguments, itemTypes );
			return itemTypes;
		}
 protected virtual Uri GetUri(Type viewType, Type viewModelType, string url, UriKind uriKind)
 {
     if (!string.IsNullOrEmpty(url))
         return new Uri(url, uriKind);
     Assembly assembly = viewType.GetAssembly();
     string name = assembly.GetAssemblyName().Name;
     string uri = viewType.FullName.Replace(name, string.Empty).Replace(".", "/");
     return new Uri(string.Format("/{0};component{1}.xaml", name, uri), uriKind);
 }
Example #4
0
		public static bool IsTuple( Type type )
		{
			var assembly = type.GetAssembly();
			return
				( assembly.Equals( typeof( object ).GetAssembly() ) ||
				assembly.Equals( typeof( Enumerable ).GetAssembly() ) )
				&& type.GetIsPublic() &&
				type.Name.StartsWith( "Tuple`", StringComparison.Ordinal );
		}
		private static bool FilterCollectionType( Type type, object filterCriteria )
		{
#if !NETSTANDARD1_1 && !NETSTANDARD1_3
#if DEBUG
			Contract.Assert( type.GetIsInterface(), "type.IsInterface" );
#endif // DEBUG
			return type.GetAssembly().Equals( typeof( Array ).GetAssembly() ) && ( type.Namespace == "System.Collections" || type.Namespace == "System.Collections.Generic" );
#else
			var typeInfo = type.GetTypeInfo();
			Contract.Assert( typeInfo.IsInterface );
			return typeInfo.Assembly.Equals( typeof( Array ).GetTypeInfo().Assembly ) && ( type.Namespace == "System.Collections" || type.Namespace == "System.Collections.Generic" );
#endif // !NETSTANDARD1_1 && !NETSTANDARD1_3
		}
Example #6
0
File: Icon.cs Project: picoe/Eto
		/// <summary>
		/// Loads an icon from a resource in the same assembly as the specified <paramref name="type"/>
		/// </summary>
		/// <returns>The icon instance.</returns>
		/// <param name="resourceName">Full name of the resource in the type's assembly.</param>
		/// <param name="type">Type of the assembly to get the resource.</param>
		public static Icon FromResource(string resourceName, Type type)
		{
			if (type == null)
				throw new ArgumentNullException("type");
			return FromResource(resourceName, type.GetAssembly());
		}
        /// <summary>
        /// Creates an object of the specified <paramref name="type"/>, passing
        /// the specified <paramref name="args"/> to its constructor.
        /// </summary>
        /// <param name="type">The type of <see cref="TestItem"/> to create.</param>
        /// <param name="args">The arguments to pass to the constructor.</param>
        /// <returns>An instance of <paramref name="type"/>.</returns>
        public object CreateInstance(Type type, params object[] args)
        {
            if (this.TrustLevel == TrustLevel.Full)
            {
                return Activator.CreateInstance(type, args);
            }

            ExceptionUtilities.Assert(this.TrustLevel == TrustLevel.Medium, "This component only accounts for full trust and medium trust.");

            if (sandbox == null)
            {
                CreateMediumTrustAppDomain();
            }

            var testItem = sandbox.CreateInstanceAndUnwrap(
                type.GetAssembly().FullName,
                type.FullName,
                false,
                BindingFlags.Default, // TODO: make this work on Win8?
                null,
                args,
                null,
                null);

            return testItem;
        }
Example #8
0
		protected virtual void WriteObject(object value, Type type)
		{
			bool appendDelim = false;

#if NETFX_CORE
            if (settings.HandleCyclicReferences && !type.IsValueType())
#else
			if (settings.HandleCyclicReferences && !type.IsValueType)
#endif
			{
				int prevIndex = 0;
				if (this.previouslySerializedObjects.TryGetValue (value, out prevIndex)) {
					this.Writer.Write(JsonReader.OperatorObjectStart);
					this.WriteObjectProperty("@ref", prevIndex);
					this.WriteLine();
					this.Writer.Write(JsonReader.OperatorObjectEnd);
					return;
				} else {
					this.previouslySerializedObjects.Add (value, this.previouslySerializedObjects.Count);
				}
			}

			this.Writer.Write(JsonReader.OperatorObjectStart);

			this.depth++;
			if (this.depth > this.settings.MaxDepth)
			{
				throw new JsonSerializationException(String.Format(JsonWriter.ErrorMaxDepth, this.settings.MaxDepth));
			}
			try
			{
				if (!String.IsNullOrEmpty(this.settings.TypeHintName))
				{
					if (appendDelim)
					{
						this.WriteObjectPropertyDelim();
					}
					else
					{
						appendDelim = true;
					}
#if NETFX_CORE
                    this.WriteObjectProperty(this.settings.TypeHintName, type.FullName + ", " + type.GetAssembly().GetName().Name);
#else
					this.WriteObjectProperty(this.settings.TypeHintName, type.FullName+", "+type.Assembly.GetName().Name);
#endif
				}

#if NETFX_CORE
                bool anonymousType = type.IsGenericType() && type.Name.StartsWith(JsonWriter.AnonymousTypePrefix);
#else
				bool anonymousType = type.IsGenericType && type.Name.StartsWith(JsonWriter.AnonymousTypePrefix);
#endif

				// serialize public properties
				PropertyInfo[] properties = type.GetProperties();
				foreach (PropertyInfo property in properties)
				{
					if (!property.CanRead) {
#if !NETFX_CORE
						if (Settings.DebugMode)
							Console.WriteLine ("Cannot serialize "+property.Name+" : cannot read");
#endif
						continue;
					}
					
					if (!property.CanWrite && !anonymousType) {
#if !NETFX_CORE
						if (Settings.DebugMode)
							Console.WriteLine ("Cannot serialize "+property.Name+" : cannot write");
#endif
						continue;
					}
					
					if (this.IsIgnored(type, property, value)) {
#if !NETFX_CORE
						if (Settings.DebugMode)
							Console.WriteLine ("Cannot serialize "+property.Name+" : is ignored by settings");
#endif
						continue;
					}
					
					if (property.GetIndexParameters ().Length != 0) {
#if !NETFX_CORE
						if (Settings.DebugMode)
							Console.WriteLine ("Cannot serialize "+property.Name+" : is indexed");
#endif
						continue;
					}
					//kb
					object propertyValue = property.GetGetMethod().Invoke(value,null);//property.GetValue(value, null);
					if (this.IsDefaultValue(property, propertyValue)) {
#if !NETFX_CORE
						if (Settings.DebugMode)
							Console.WriteLine ("Cannot serialize "+property.Name+" : is default value");
#endif
						continue;
					}
				
					if (appendDelim)
						this.WriteObjectPropertyDelim();
					else
						appendDelim = true;

					// use Attributes here to control naming
					string propertyName = JsonNameAttribute.GetJsonName(property);
					if (String.IsNullOrEmpty(propertyName))
						propertyName = property.Name;

					this.WriteObjectProperty(propertyName, propertyValue);
				}

				// serialize public fields
				FieldInfo[] fields = type.GetFields();
				foreach (FieldInfo field in fields)
				{
					if (!field.IsPublic || field.IsStatic)
                    {
#if !NETFX_CORE
                        if (Settings.DebugMode)
							Console.WriteLine ("Cannot serialize "+field.Name+" : not public or is static");
#endif
                        continue;
					}
					
					if (this.IsIgnored(type, field, value)) {
#if !NETFX_CORE
						if (Settings.DebugMode)
							Console.WriteLine ("Cannot serialize "+field.Name+" : ignored by settings");
				
#endif
                        continue;
					}

					object fieldValue = field.GetValue(value);
					if (this.IsDefaultValue(field, fieldValue)) {
#if !NETFX_CORE
						if (Settings.DebugMode)
							Console.WriteLine ("Cannot serialize "+field.Name+" : is default value");
#endif
						continue;

					}

					if (appendDelim)
					{
						this.WriteObjectPropertyDelim();
						this.WriteLine();
					} else
					{
						appendDelim = true;
					}
					
					// use Attributes here to control naming
					string fieldName = JsonNameAttribute.GetJsonName(field);
					if (String.IsNullOrEmpty(fieldName))
						fieldName = field.Name;

					this.WriteObjectProperty(fieldName, fieldValue);
				}
			}
			finally
			{
				this.depth--;
			}

			if (appendDelim)
				this.WriteLine();
			
			this.Writer.Write(JsonReader.OperatorObjectEnd);
		}
		private static void WriteType( TextWriter writer, Type type )
		{
			Contract.Assert( writer != null );
			if ( writer == NullTextWriter.Instance )
			{
				return;
			}

			if ( type == null || type == typeof( void ) )
			{
				writer.Write( "void" );
			}
			else if ( type.IsGenericParameter )
			{
				writer.Write( "{0}{1}", type.GetDeclaringMethod() == null ? "!" : "!!", type.Name );
			}
			else
			{
#if SILVERLIGHT
				var endOfAssemblySimpleName = type.Assembly.FullName.IndexOf( ',' );
				writer.Write( "[{0}]{1}", endOfAssemblySimpleName < 0 ? type.Assembly.FullName : type.Assembly.FullName.Remove( endOfAssemblySimpleName ), type.FullName );
#else
				writer.Write( "[{0}]{1}", type.GetAssembly().GetName().Name, type.GetFullName() );
#endif
			}
		}
 public ResourceVirtualPathProvider(IAppHost appHost, Type baseTypeInAssmebly)
     : this(appHost, baseTypeInAssmebly.GetAssembly(), GetNamespace(baseTypeInAssmebly)) { }
 protected virtual Uri GetUri(Type viewType, Type viewModelType, string url, UriKind uriKind)
 {
     if (!string.IsNullOrEmpty(url))
         return new Uri(url, uriKind);
     if (!IsSupportedUriNavigation)
         return ViewMappingItem.Empty;
     Assembly assembly = viewType.GetAssembly();
     string name = assembly.GetAssemblyName().Name;
     string uri = viewType.FullName.Replace(name, string.Empty).Replace(".", "/");
     return new Uri($"/{name};component{uri}.xaml", uriKind);
 }