对于C#来说,指针是没有任何意义的,写这个完全是爱好.业务的时候,写了个测试指针的类.测试了一下.感觉还可以
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace Test
{
/// <summary>
/// 测试C#数据存取
/// </summary>
class TestModel
{
struct TestStruct
{
public int a ;
public string b;
}
private char[] str1 = "1".ToCharArray();
private char[] str2 = "2".ToCharArray();
public static TestModel kkk = new TestModel();
public static void test()
{
TestStruct mm = new TestStruct();
test2 kk = new test2();
System.Console.WriteLine(Convert.ToString((Int64)Marshal.GetIUnknownForObject(mm)));
System.Console.WriteLine(Convert.ToString((Int64)Marshal.GetIUnknownForObject(kk)));
System.Console.ReadLine();
}
public static void testStruct()
{
TestStruct ttt = new TestStruct();
TestModel t = TestModel.kkk;
ttt.a = 1;
char[] str1 ="3".ToCharArray();
char[] str2 ="4".ToCharArray();
ttt.b = "5";
unsafe
{
IntPtr ptr = Marshal.GetIUnknownForObject(ttt.b);
System.Console.WriteLine(Convert.ToString((Int64)ptr));
IntPtr ptr3 = Marshal.GetIUnknownForObject(str1);
System.Console.WriteLine(Convert.ToString((Int64)ptr3));
IntPtr ptr2 = Marshal.GetIUnknownForObject(str2);
System.Console.WriteLine(Convert.ToString((Int64)ptr2));
System.Console.WriteLine("t.str1="+Convert.ToString((Int64)Marshal.GetIUnknownForObject(t.str1)));
System.Console.WriteLine("t.str2="+Convert.ToString((Int64)Marshal.GetIUnknownForObject(t.str2)));
System.Console.WriteLine(Convert.ToString((Int64)Marshal.GetIUnknownForObject(t)));
int a1 =2;
int a3 = 3;
int a4 = 4;
int a5 = 5;
System.Console.WriteLine(Convert.ToString((Int64)(&a1)));
System.Console.WriteLine(Convert.ToString((Int64)(&a3)));
System.Console.WriteLine(Convert.ToString((Int64)(&a4)));
System.Console.WriteLine(Convert.ToString((Int64)(&a5)));
}
System.Console.WriteLine(Convert.ToString(Marshal.SizeOf(ttt)));
System.Console.ReadLine();
}
public unsafe static void TestString()
{
string a = "abc";
System.Console.WriteLine(Convert.ToString((Int64)Marshal.GetIUnknownForObject(a)));
test(a);
System.Console.ReadLine();
}
public unsafe static void test(string str)
{
IntPtr ptr = Marshal.GetIUnknownForObject(str);
str = "123";
object t = Marshal.GetObjectForIUnknown(ptr);
System.Console.WriteLine(t.ToString());
System.Console.WriteLine(Convert.ToString((Int64)Marshal.GetIUnknownForObject(str)));
}
}
public class test2
{
int a;
int b;
string a2;
string b2 = "abc";
}
}