本文整理汇总了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());
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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();
}
示例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;
}
示例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;
}
示例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";
}
示例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);
}
示例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;
}
示例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;
}
示例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());
}
示例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;
}
示例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;
}