本文整理汇总了C#中Array.GetType方法的典型用法代码示例。如果您正苦于以下问题:C# Array.GetType方法的具体用法?C# Array.GetType怎么用?C# Array.GetType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Array
的用法示例。
在下文中一共展示了Array.GetType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: concat
public static Array concat( Array lhs,Array rhs )
{
Array res=Array.CreateInstance( lhs.GetType().GetElementType(),lhs.Length+rhs.Length );
Array.Copy( lhs,0,res,0,lhs.Length );
Array.Copy( rhs,0,res,lhs.Length,rhs.Length );
return res;
}
示例2: resizeArray
public static Array resizeArray( Array arr,int len )
{
Type ty=arr.GetType().GetElementType();
Array res=Array.CreateInstance( ty,len );
int n=Math.Min( arr.Length,len );
if( n>0 ) Array.Copy( arr,res,n );
return res;
}
示例3: ArrayRankInfo
private static void ArrayRankInfo(String name, Array a) {
Console.WriteLine("Number of dimensions in \"{0}\" array (of type {1}): ",
name, a.GetType().ToString(), a.Rank);
for (int r = 0; r < a.Rank; r++) {
Console.WriteLine("Rank: {0}, LowerBound = {1}, UpperBound = {2}",
r, a.GetLowerBound(r), a.GetUpperBound(r));
}
Console.WriteLine();
}
示例4: CombineArrays
private static Array CombineArrays(Array arr1, Array arr2)
{
if (arr1 == null)
return arr2;
if (arr2 == null)
return arr1;
Array newArr = Array.CreateInstance(arr1.GetType().GetElementType(), arr1.Length + arr2.Length);
arr1.CopyTo(newArr, 0);
arr2.CopyTo(newArr, arr1.Length);
return newArr;
}
示例5: GetArraySlice
public static object GetArraySlice( Array inputarray, int sliceindex )
{
int numoutputdimensions = inputarray.Rank - 1;
int[] newdimensions = new int[ numoutputdimensions ];
for( int i = 1; i < numoutputdimensions + 1; i++ )
{
newdimensions[ i - 1 ] = inputarray.GetUpperBound( i ) + 1;
}
Array newarray = Array.CreateInstance( inputarray.GetType().GetElementType(), newdimensions );
int[]traverseinputindex = new int[ numoutputdimensions + 1 ];
int[]traverseoutputindex = new int[ numoutputdimensions ];
traverseinputindex[0] = sliceindex;
bool bDone = false;
while( !bDone )
{
newarray.SetValue( inputarray.GetValue( traverseinputindex ), traverseoutputindex );
bool bUpdatedtraverseindex = false;
for( int i = numoutputdimensions - 1; i >= 0 && !bUpdatedtraverseindex; i-- )
{
traverseinputindex[i + 1]++;
traverseoutputindex[i]++;
if( traverseoutputindex[i] >= newdimensions[i] )
{
if( i == 0 )
{
bDone = true;
}
else
{
traverseinputindex[i + 1] = 0;
traverseoutputindex[i ] = 0;
}
}
else
{
bUpdatedtraverseindex = true;
}
}
}
return newarray;
}
示例6: InitializeArray
// TODO: Implement this method in the compiler
public static void InitializeArray(Array array, RuntimeFieldHandle fldHandle)
{
Logging.Debug("Initializing array " + array + " with value " + fldHandle);
RuntimeType type = (RuntimeType)array.GetType();
if (!type.Class.IsArray)
{
throw new SystemException("Array was not marked with class that was an array. Class: " + type.Class);
}
if (type.Class.ElementClass == null)
{
throw new SystemException("Array was not marked with class that did not have an element class. Class: " + type.Class);
}
Type elementType = Type.GetTypeFromHandle(((RuntimeTypeHandle)type.Class.ElementClass.VTable));
if (elementType == Type.GetType("System.Int32"))
{
int[] intArray = var.Cast<Array>(array).Cast<int[]>();
byte[] initialValue = var.Cast<RuntimeFieldHandle>(fldHandle).Cast<byte[]>();
InitializeInt32Array(intArray, initialValue);
}
else
{
throw new SystemException("Cannot initialize array of type: " + elementType);
}
}
示例7: ByteLength
public unsafe static int ByteLength(Array array)
{
if (array == null) throw new ArgumentNullException("array");
Type.TypeData* typeData = array.GetType().GetTypeDataPointer();
return (int)typeData->ArrayElementType->StackSize * array.Length;
}
示例8: CreateBinaryArrayNode
private YamlNode CreateBinaryArrayNode(Array array)
{
var type = array.GetType();
var element = type.GetElementType();
if ( !TypeUtils.IsPureValueType(element) )
throw new InvalidOperationException(
"Can not serialize {0} as binary because it contains non-value-type(s).".DoFormat(type.FullName));
var elementSize = Marshal.SizeOf(element);
var binary = new byte[array.LongLength * elementSize];
int j = 0;
for ( int i = 0; i < array.Length; i++ ) {
IntPtr p = Marshal.UnsafeAddrOfPinnedArrayElement(array, i);
Marshal.Copy(p, binary, j, elementSize);
j += elementSize;
}
var dimension = "";
if ( array.Rank > 1 ) {
for ( int i = 0; i < array.Rank; i++ ) {
if ( dimension != "" )
dimension += ", ";
dimension += array.GetLength(i);
}
dimension = "[" + dimension + "]\r\n";
}
var result= str(TypeNameToYamlTag(type), dimension + Base64Encode(type, binary));
result.Properties["Don'tCareLineBreaks"] = "true";
return result;
}
示例9: CreateArrayNodeSub
private YamlNode CreateArrayNodeSub(Array array, int i, long[] indices)
{
var type= array.GetType();
var element = type.GetElementType();
var sequence = seq();
if ( i == 0 ) {
sequence.Tag = TypeNameToYamlTag(type);
AppendToAppeared(array, sequence);
}
if ( element.IsPrimitive || element.IsEnum || element == typeof(decimal) )
if ( array.Rank == 1 || ArrayLength(array, i+1) < 20 )
sequence.Properties["Compact"] = "true";
for ( indices[i] = 0; indices[i] < array.GetLength(i); indices[i]++ )
if ( i == array.Rank - 1 ) {
var n = ObjectToNode(array.GetValue(indices), type.GetElementType());
sequence.Add(n);
} else {
var s = CreateArrayNodeSub(array, i + 1, indices);
sequence.Add(s);
}
return sequence;
}
示例10: CreateArrayNode
private YamlNode CreateArrayNode(Array array)
{
Type type = array.GetType();
return CreateArrayNodeSub(array, 0, new long[type.GetArrayRank()]);
}
示例11: Write
public void Write(Array structures)
{
List<Tuple<int, TypeCode>> parser;
Type type;
byte[] data;
type = structures.GetType().GetElementType();
parser = GetParser(type);
data = new byte[Marshal.SizeOf(type)];
using (var ms = new MemoryStream(data))
{
using (var rd = new BinaryReader(ms))
{
foreach (var structure in structures)
{
ms.Seek(0, SeekOrigin.Begin);
Marshal.StructureToPtr(structure, Marshal.UnsafeAddrOfPinnedArrayElement(data, 0), true);
RunParser(parser, rd);
}
}
}
}
示例12: ReflectThroughConvert
private bool ReflectThroughConvert(Array source, Array target, int iArrayElement)
{
Type tpTargetArrayElement = target.GetType().GetElementType();
String strToXXX = "To" + tpTargetArrayElement.Name.Trim();
Type tpConvert = typeof(System.Convert);
Object[] vntSourceElemValue = new Object[1];
vntSourceElemValue[0] = source.GetValue(iArrayElement);
Object vntConvertedSourceValue = null;
try
{
vntConvertedSourceValue = tpConvert.InvokeMember(strToXXX, BindingFlags.InvokeMethod, null, null, vntSourceElemValue);
}catch(MissingMethodException) {
return false;
}
Object vntTargetValue = target.GetValue(iArrayElement);
if(!vntConvertedSourceValue.Equals(vntTargetValue))
{
Console.WriteLine();
Console.WriteLine("reflected target = " + vntConvertedSourceValue + ", type = " + vntConvertedSourceValue.GetType() + ", strToXXX = " + strToXXX);
Console.WriteLine("real target = " + vntTargetValue + ", type = " + vntTargetValue.GetType());
Console.WriteLine("source array type = " + source.GetType().GetElementType() + ", target array type = " + target.GetType().GetElementType());
Console.WriteLine();
}
return false;
}
示例13: Fill
/// <summary>
/// Fills the array with an specific value from an specific index to an specific index.
/// </summary>
/// <param name="array">The array to be filled.</param>
/// <param name="fromindex">The first index to be filled.</param>
/// <param name="toindex">The last index to be filled.</param>
/// <param name="val">The value to fill the array with.</param>
public static void Fill(Array array, Int32 fromindex, Int32 toindex, Object val)
{
Object Temp_Object = val;
Type elementtype = array.GetType().GetElementType();
if (elementtype != val.GetType())
Temp_Object = Convert.ChangeType(val, elementtype);
if (array.Length == 0)
throw (new NullReferenceException());
if (fromindex > toindex)
throw (new ArgumentException());
if ((fromindex < 0) || ((Array)array).Length < toindex)
throw (new IndexOutOfRangeException());
for (int index = (fromindex > 0) ? fromindex-- : fromindex; index < toindex; index++)
array.SetValue(Temp_Object, index);
}
示例14: resizeArrayArray
public static Array[] resizeArrayArray( Array[] arr,int len )
{
int i=arr.Length;
arr=(Array[])resizeArray( arr,len );
if( i<len ){
Array empty=Array.CreateInstance( arr.GetType().GetElementType().GetElementType(),0 );
while( i<len ) arr[i++]=empty;
}
return arr;
}
示例15: compareSequence
// Arrays have only one dimension
static bool compareSequence(Array ar1, Array ar2)
{
Debug.Assert(ar1 != null && ar2 != null);
Type t1 = ar1.GetType();
Type t2 = ar2.GetType();
if (!(ar1.Rank == 1 && ar2.Rank == 1
&& ar1.Length == ar2.Length && t1.GetElementType() == t2.GetElementType()))
return false;
//arrays have same rank and size and element type.
int len = ar1.Length;
Type elemType = t1.GetElementType();
bool ret = true;
for (int i = 0; i < len; i++)
{
if (compareData(ar1.GetValue(i), ar2.GetValue(i)) == false)
{
ret = false;
break;
}
}
return ret;
}