當前位置: 首頁>>代碼示例>>C#>>正文


C# Type.GetElementType方法代碼示例

本文整理匯總了C#中System.Type.GetElementType方法的典型用法代碼示例。如果您正苦於以下問題:C# Type.GetElementType方法的具體用法?C# Type.GetElementType怎麽用?C# Type.GetElementType使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Type的用法示例。


在下文中一共展示了Type.GetElementType方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: ToCSharpString

		private static void ToCSharpString(Type type, StringBuilder name)
		{
			if (type.IsArray)
			{
				var elementType = type.GetElementType();
				ToCSharpString(elementType, name);
				name.Append(type.Name.Substring(elementType.Name.Length));
				return;
			}
			if (type.IsGenericParameter)
			{
				//NOTE: this has to go before type.IsNested because nested generic type is also a generic parameter and otherwise we'd have stack overflow
				name.AppendFormat("·{0}·", type.Name);
				return;
			}
			if (type.IsNested)
			{
				ToCSharpString(type.DeclaringType, name);
				name.Append(".");
			}
			if (type.IsGenericType == false)
			{
				name.Append(type.Name);
				return;
			}
			name.Append(type.Name.Split('`')[0]);
			AppendGenericParameters(name, type.GetGenericArguments());
		}
開發者ID:CV1,項目名稱:Windsor,代碼行數:28,代碼來源:TypeUtil.cs

示例2: IsSimilarType

        /// <summary>
        /// Determines if the two types are either identical, or are both generic parameters or generic types
        /// with generic parameters in the same locations (generic parameters match any other generic paramter,
        /// but NOT concrete types).
        /// </summary>
        private static bool IsSimilarType(this Type thisType, Type type)
        {
            // Ignore any 'ref' types
            if (thisType.IsByRef)
                thisType = thisType.GetElementType();
            if (type.IsByRef)
                type = type.GetElementType();

            // Handle array types
            if (thisType.IsArray && type.IsArray)
                return thisType.GetElementType().IsSimilarType(type.GetElementType());

            // If the types are identical, or they're both generic parameters or the special 'T' type, treat as a match
            if (thisType == type || ((thisType.IsGenericParameter || thisType == typeof(T)) && (type.IsGenericParameter || type == typeof(T))))
                return true;

            // Handle any generic arguments
            if (thisType.IsGenericType && type.IsGenericType)
            {
                var thisArguments = thisType.GetGenericArguments();
                var arguments = type.GetGenericArguments();
                if (thisArguments.Length == arguments.Length)
                {
                    for (var i = 0; i < thisArguments.Length; ++i)
                    {
                        if (!thisArguments[i].IsSimilarType(arguments[i]))
                            return false;
                    }
                    return true;
                }
            }

            return false;
        }
開發者ID:elondon,項目名稱:IBuildObjects,代碼行數:39,代碼來源:ReflectionExtensions.cs

示例3: GetClosedParameterType

		public static Type GetClosedParameterType(AbstractTypeEmitter type, Type parameter)
		{
			if (parameter.IsGenericTypeDefinition)
			{
				return parameter.GetGenericTypeDefinition().MakeGenericType(type.GetGenericArgumentsFor(parameter));
			}

			if (parameter.IsGenericType)
			{
				Type[] arguments = parameter.GetGenericArguments();
				if (CloseGenericParametersIfAny(type, arguments))
				{
					return parameter.GetGenericTypeDefinition().MakeGenericType(arguments);
				}
			}
			
			if (parameter.IsGenericParameter)
			{
				return type.GetGenericArgument(parameter.Name);
			}

			if (parameter.IsArray)
			{
				var elementType = GetClosedParameterType(type, parameter.GetElementType());
				return elementType.MakeArrayType();
			}
			
			if(parameter.IsByRef)
			{
				var elementType = GetClosedParameterType(type, parameter.GetElementType());
				return elementType.MakeByRefType();
			}

			return parameter;
		}
開發者ID:JulianBirch,項目名稱:Castle.Core,代碼行數:35,代碼來源:TypeUtil.cs

示例4: FromIiop

        public static string FromIiop(Type type, string name)
        {
            if (type == typeof(void)) return "";
            if (type.IsByRef)
            {
                return FromIiop(type.GetElementType(), name);
            }

            if (type.IsArray)
            {
                if (type.GetElementType() == typeof(global::org.omg.SDOPackage.NameValue))
                {
                    return "Converter.NVListToDictionary(" + name + ")";
                }
                return name + ".Select(x => (" + GetFullRefTypeName(type.GetElementType()) + ")" + FromIiop(type.GetElementType(), "x") + ").ToList()";
            }

            if (IsPrimitive(type)) return name;
            if (type == typeof(RTC.Time)) return "Converter.RtcTimeToDateTime(" + name + ")";
            if (IsStruct(type)) return "new " + GetFullDataName(type) + "(" + name + ")";
            if (IsInterface(type)) return "new " + GetFullName(type) + "Stub(" + name + ")";
            if (IsUnion(type)) return name;
            if (type == typeof(MarshalByRefObject)) return "(object)" + name;

            //if (IsEnum(type))
            return "(" + GetFullName(type)+ ")" + name;
        }
開發者ID:zoetrope,項目名稱:ReactiveRTM,代碼行數:27,代碼來源:GeneratorUtility.cs

示例5: Deserialize

        public object Deserialize(string content, Type objectType)
        {
            var xmlDocument = new XmlDocument();
            xmlDocument.LoadXml(content);

            var contents = xmlDocument.DocumentElement.GetElementsByTagName("content");

            if(objectType.IsAResource())
            {
                return ToObject(contents[0].InnerText, objectType);
            }

            if(objectType.IsAListOfResources())
            {
                var list = Array.CreateInstance(objectType.GetElementType(), contents.Count);
                for(var i = 0; i < contents.Count; i++)
                {
                    list.SetValue(ToObject(contents[i].InnerText, objectType.GetElementType()), i);
                }

                return list;
            }

            return null;
        }
開發者ID:seixasfelipe,項目名稱:restfulie.net,代碼行數:25,代碼來源:AtomPlusXmlDeserializer.cs

示例6: GetSafeTypeName

        public static string GetSafeTypeName(Type type)
        {
            if (type.IsByRef)
            {
                return GetSafeTypeName(type.GetElementType());
            }
            if (type.IsArray)
            {
                return GetSafeTypeName(type.GetElementType()) + "[]";
            }
            if (!type.IsGenericType)
            {
                return (type.IsGenericParameter) ? type.Name : GetTypePrimitiveName(type);
            }

            var generic = type.GetGenericTypeDefinition();
            var sb = new StringBuilder();
            sb.Append(generic.Name.Substring(0, generic.Name.IndexOf('`')));
            sb.Append('<');

            int i = 0;
            foreach (var arg in type.GetGenericArguments())
            {
                if (i++ > 0)
                {
                    sb.Append(", ");
                }
                sb.Append(GetSafeTypeName(arg));
            }
            sb.Append('>');
            return sb.ToString();
        }
開發者ID:pcsikos,項目名稱:Tools,代碼行數:32,代碼來源:CSharpSourceCodeGeneration.cs

示例7: ParseValue

        public static object ParseValue(string rawValue, Type type)
        {
            if (string.IsNullOrWhiteSpace(rawValue))
                throw new ArgumentNullException("rawValue");

            if (type == null)
                throw new ArgumentNullException("type");

            object value;

            if (type.IsArray)
            {
                string[] elements = rawValue.Replace("[", string.Empty).Replace("]", string.Empty).Split(',');
                Array array = Array.CreateInstance(type.GetElementType(), elements.Length);

                for (int i = 0; i < array.Length; i++)
                    array.SetValue(Convert.ChangeType(elements[i], type.GetElementType()), i);

                value = array;
            }
            else
                value = Convert.ChangeType(rawValue, type);

            return value;
        }
開發者ID:swemaniac,項目名稱:ExtPlaneNet,代碼行數:25,代碼來源:DataRef.cs

示例8: GetArrayModel

 protected virtual object GetArrayModel( ControllerContext controllerContext, Type modelType, IValueProvider valueProvider, string prefix)
 {
     List<object> list = GetListModel(controllerContext, modelType, modelType.GetElementType(), valueProvider, prefix);
     object[] array = (object[])Array.CreateInstance(modelType.GetElementType(), list.Count);
     list.CopyTo(array);
     return array;
 }  
開發者ID:xiaohong2015,項目名稱:.NET,代碼行數:7,代碼來源:DefaultModelBinder.cs

示例9: RecursiveArray

        //Get some default data
        //Walk over an array
        public string RecursiveArray(object obj, Type t, string[] tokens, int index)
        {
            //got to the last token, but still have structure below this node
            if (index >= tokens.Length)
                return "Type is complex. Cannot show as string";

            //Got to an index in the array that is null
            if (obj == null)
                return "Array is null";

            //If the type is an array
            if (t.FullName.Contains("System.Collections.Generic.List") == true)
            {
                Array array = ((Array)(t.GetMethod("ToArray").Invoke(obj, null)));

                //if you requested the length, return the length
                if (tokens[index] == "length")
                    return array.Length.ToString();

                //might have a bad index format
                try
                {
                    //try to get the current token as a int
                    int idx = System.Convert.ToInt32(tokens[index]);

                    //check the bounds
                    if (idx >= array.Length)
                        return "index out of bounds";

                    //Get the object at this position in the array
                    object i = array.GetValue(idx);
                    //If the object in this array is null
                    if (i == null)
                    {
                        //if its a string, create a string
                        if (t.GetElementType() == typeof(String))
                            array.SetValue("", idx);
                        //else create an object of whatever it was suposed to be
                        else
                            array.SetValue(Activator.CreateInstance(t.GetElementType()), idx);
                    }
                    //Get the new value after null check
                    i = array.GetValue(idx);

                    //If its an array
                    if (i.GetType().FullName.Contains("System.Collections.Generic.List") == true)
                        return RecursiveArray(i, i.GetType(), tokens, index + 1);
                    else
                        return RecursiveGet(i, i.GetType(), tokens, index + 1);
                }
                //The format of the array index was incorrect
                catch (System.FormatException e)
                {
                    return "Array index invalid";
                }
            }
            //shoudl not get here, but if we did, then there is no object at this level with the
            //same name as the token
            return "Error parsing datamodel parameter string";
        }
開發者ID:AntoineOctarina,項目名稱:scorm4unity,代碼行數:62,代碼來源:ScormDatamodelCommandParser.cs

示例10: AppendPrettyNameCore

        private static StringBuilder AppendPrettyNameCore(this StringBuilder name, Type type, PrettyNameContext context)
        {
            // Suffixes (array, ref, pointer)
            if (type.IsArray)
                return name
                    .AppendPrettyName(type.GetElementType(), context)
                    .Append('[')
                    .Append(',', type.GetArrayRank() - 1)
                    .Append(']');
            else if (type.IsByRef)
                return name
                    .AppendPrettyName(type.GetElementType(), context)
                    .Append('&');
            else if (type.IsPointer)
                return name
                    .AppendPrettyName(type.GetElementType(), context)
                    .Append('*');

            // Prefixes (nesting, namespace)
            if (type.IsNested)
                name.AppendPrettyNameCore(type.DeclaringType, context)
                    .Append('.');
            else if (context.IsQualified)
                name.Append(type.Namespace)
                    .Append('.');

            // Name and arguments
            if (type.IsGenericType)
                return name
                    .Append(type.Name.WithoutGenericSuffix())
                    .AppendPrettyArguments(type, context);
            else
                return name
                    .Append(type.Name);
        }
開發者ID:sharpjs,項目名稱:Projector,代碼行數:35,代碼來源:TypePrettyName.cs

示例11: GetFullRefTypeName

        public static string GetFullRefTypeName(Type type)
        {
            if (type == typeof(void)) return "void";
            if (type.IsByRef)
            {
                return GetFullRefTypeName(type.GetElementType());
            }
            if (type.IsArray)
            {
                if (type.GetElementType() == typeof(global::org.omg.SDOPackage.NameValue))
                {
                    return "Dictionary<string,object>";
                }
                //return "ObservableCollection<" + GetFullRefTypeName(type.GetElementType()) + ">";
                return "List<" + GetFullRefTypeName(type.GetElementType()) + ">";
            }

            if (type == typeof(RTC.Time)) return typeof(DateTime).FullName;
            if (IsPrimitive(type)) return type.FullName;
            if (IsEnum(type) || IsStruct(type)) return GetFullDataName(type);
            if (IsInterface(type)) return GetFullName(type);
            if (IsUnion(type)) return GetIiopName(type);

            return typeof(object).FullName;
        }
開發者ID:zoetrope,項目名稱:ReactiveRTM,代碼行數:25,代碼來源:GeneratorUtility.cs

示例12: CreateDefaultType

        private object CreateDefaultType(Type t)
        {
            object ret = null;

            try
            {
                if (t.GetElementType() != null)
                {
                    t = t.GetElementType();
                }
               
                if (t == typeof(string))
                {
                    ret = String.Empty;
                }
                else if (t == typeof(IBindCtx))
                {
                    ret = COMUtilities.CreateBindCtx(0);
                }
                else
                {
                    /* Try the default activation route */
                    ret = System.Activator.CreateInstance(t);
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
            }

            return ret;
        }
開發者ID:asymmetricsec,項目名稱:oleviewdotnet,代碼行數:32,代碼來源:InvokeForm.cs

示例13: GetService

 public object GetService(Type serviceType, Type interfaceType, IServiceLocator context)
 {
     ArrayList list = new ArrayList();
     foreach (IServiceProvider subsystem in subsystems)
     {
         list.Add(subsystem.GetService(serviceType.GetElementType(), interfaceType.GetElementType(), context));
     }
     return list.ToArray(interfaceType.GetElementType());
 }
開發者ID:NigelThorne,項目名稱:ndependencyinjection,代碼行數:9,代碼來源:CollectionProvider.cs

示例14: MatchesType

 public override bool MatchesType(Type t)
 {
     Guard.ArgumentNotNull(t, "t");
     if (!isArray)
     {
         return t.IsGenericParameter && t.Name == genericParameterName;
     }
     return t.IsArray && t.GetElementType().IsGenericParameter && t.GetElementType().Name == genericParameterName;
 }
開發者ID:shhyder,項目名稱:MapApplication,代碼行數:9,代碼來源:GenericParameterBase.cs

示例15: FitTypeForExternalUse

 public static object FitTypeForExternalUse(object o, Type t)
 {
     Core.DeReference(ref o);
     // if type of object is desired type or subtype, fine
     if (o.GetType() == t || o.GetType().IsSubclassOf(t))
         return o;
     // try to convert an number desired number type
     else if (o is int || o is double) {
         if (t == typeof(int))
             return System.Convert.ToInt32(o);
         else if (t == typeof(uint))
             return System.Convert.ToUInt32(o);
         else if (t == typeof(long))
             return System.Convert.ToInt64(o);
         else if (t == typeof(ulong))
             return System.Convert.ToUInt64(o);
         else if (t == typeof(short))
             return System.Convert.ToInt16(o);
         else if (t == typeof(ushort))
             return System.Convert.ToUInt16(o);
         else if (t == typeof(byte))
             return System.Convert.ToByte(o);
         else if (t == typeof(sbyte))
             return System.Convert.ToSByte(o);
         else if (t == typeof(bool))
             return System.Convert.ToBoolean(o);
         else if (t == typeof(float))
             return System.Convert.ToSingle(o);
         else if (t == typeof(double))
             return System.Convert.ToDouble(o);
         else if (t == typeof(decimal))
             return System.Convert.ToDecimal(o);
         else
             return null;
     }
     // try to convert a string to the desired string type
     else if (o is string) {
         if (t == typeof(char))
             return System.Convert.ToChar((string)o);
         else
             return null;
     }
     // try to convert an Array to the desired array type
     else if (o is Array) {
         if (t == typeof(System.Array) || t.IsArray) {
             Array arr = (Array)o;
             System.Array result = System.Array.CreateInstance(t.GetElementType(), arr.Values.Count);
             for (int i = 0; i < arr.Values.Count; i++)
                 result.SetValue(FitTypeForExternalUse(arr.Values[i], t.GetElementType()), i);
             return result;
         }
         else
             return null;
     }
     else
         return null;
 }
開發者ID:Notulp,項目名稱:php4mono,代碼行數:57,代碼來源:Convert.cs


注:本文中的System.Type.GetElementType方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。