当前位置: 首页>>代码示例>>C#>>正文


C# Class.Call方法代码示例

本文整理汇总了C#中Class.Call方法的典型用法代码示例。如果您正苦于以下问题:C# Class.Call方法的具体用法?C# Class.Call怎么用?C# Class.Call使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Class的用法示例。


在下文中一共展示了Class.Call方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: FromManaged

    public void FromManaged()
    {
        NSObject s = new Class("Subclass1").Call("alloc").Call("init").To<NSObject>();

        try
        {
            Managed.LogException = (e) => {};
            s.Call("BadValue");
            Assert.Fail("should have been an exception");
        }
        catch (TargetInvocationException e)
        {
            Assert.IsTrue(e.Message.Contains("Exception has been thrown by the (managed) target of an Objective-C method call"));
            Assert.IsNotNull(e.InnerException);

            ArgumentException ae = e.InnerException as ArgumentException;
            Assert.IsNotNull(ae);
            Assert.IsTrue(ae.Message.Contains("my error"));
            Assert.IsTrue(ae.Message.Contains("alpha"));
            Assert.AreEqual("alpha", ae.ParamName);
        }
        finally
        {
            Managed.LogException = null;
        }
    }
开发者ID:afrog33k,项目名称:mobjc,代码行数:26,代码来源:ExceptionTests.cs

示例2: ArrayArg

    public void ArrayArg()
    {
        NSObject pool = new NSObject(NSObject.AllocAndInitInstance("NSAutoreleasePool"));
        Class nsData = new Class("NSData");
        long bytes = DoGetMemory();

        for (int j = 1; j < 100; ++j)
        {
            for (int i = 0; i < NumIterations/100; ++i)
            {
                byte[] data = new byte[]{2, 5, 6, 3};

                NSObject d = new NSObject(nsData.Call("alloc"));
                NSObject e = (NSObject) d.Call("initWithBytes:length:", data, data.Length);
                e.release();
            }
            GC.Collect();
        }

        pool.release();
        GC.Collect();
        GC.WaitForPendingFinalizers();

        long delta = DoGetMemory() - bytes;
        if (delta/NumIterations > 4)
            Assert.Fail("ArrayArg used {0}K of memory ({1} bytes per iteration)!", delta/1024, delta/NumIterations);
    }
开发者ID:afrog33k,项目名称:mobjc,代码行数:27,代码来源:MemoryTests.cs

示例3: App

    public App(string nibName)
    {
        NSObject app = (NSObject) new Class("NSApplication").Call("sharedApplication");

        // Load our nib. This will instantiate all of the native objects and wire them together.
        // The C# SimpleLayoutView will be created the first time Objective-C calls one of the
        // methods SimpleLayoutView added or overrode.
        NSObject dict = new Class("NSMutableDictionary").Call("alloc").Call("init").To<NSObject>();
        NSObject key = new Class("NSString").Call("stringWithUTF8String:", Marshal.StringToHGlobalAuto("NSOwner")).To<NSObject>();
        dict.Call("setObject:forKey:", app, key);

        NSObject bundle = new Class("NSBundle").Call("mainBundle").To<NSObject>();

        NSObject nib = new Class("NSString").Call("stringWithUTF8String:", Marshal.StringToHGlobalAuto(nibName)).To<NSObject>();
        sbyte loaded = (sbyte) bundle.Call("loadNibFile:externalNameTable:withZone:", nib, dict, null);
        if (loaded != 1)
            throw new InvalidOperationException("Couldn't load the nib file");

        // We need an NSAutoreleasePool to do Native.Call, but we don't want to have one
        // hanging around while we're in the main event loop because that may hide bugs.
        // So, we'll instantiate a Native instance here and call Invoke later which can
        // be done without an NSAutoreleasePool.
        m_run = new Native(app, new Selector("run"));

        dict.release();
    }
开发者ID:afrog33k,项目名称:mobjc,代码行数:26,代码来源:NSApplication.cs

示例4: BadStruct

    public void BadStruct()
    {
        NSObject str = new Class("NSString").Call("stringWithUTF8String:", Marshal.StringToHGlobalAuto("the quick brown fox")).To<NSObject>();

        NSRect r = new NSRect();
        str.Call("substringWithRange:", r);
    }
开发者ID:afrog33k,项目名称:mobjc,代码行数:7,代码来源:ArgTests.cs

示例5: BadUInt16To

    public void BadUInt16To()
    {
        Class klass = new Class("NSNumber");

        NSObject num = klass.Call("alloc").To<NSObject>();
        num = num.Call("initWithInt:", 5000).To<NSObject>();

        num.Call("unsignedShortValue").To<Int32>();
    }
开发者ID:afrog33k,项目名称:mobjc,代码行数:9,代码来源:ReturnTests.cs

示例6: BadFloatTo

    public void BadFloatTo()
    {
        Class klass = new Class("NSNumber");

        NSObject num = klass.Call("alloc").To<NSObject>();
        num = num.Call("initWithInt:", 5000).To<NSObject>();

        num.Call("floatValue").To<double>();
    }
开发者ID:afrog33k,项目名称:mobjc,代码行数:9,代码来源:ReturnTests.cs

示例7: BadLongTo

    public void BadLongTo()
    {
        Class klass = new Class("NSNumber");

        NSObject num = klass.Call("alloc").To<NSObject>();
        num = num.Call("initWithInt:", 5000).To<NSObject>();

        num.Call("longValue").To<Int64>();
    }
开发者ID:afrog33k,项目名称:mobjc,代码行数:9,代码来源:ReturnTests.cs

示例8: Time

    public void Time()
    {
        Class klass = new Class("NSString");
        NSObject str1 = (NSObject) klass.Call("stringWithUTF8String:", Marshal.StringToHGlobalAuto("hello world"));
        NSObject str2 = (NSObject) klass.Call("stringWithUTF8String:", Marshal.StringToHGlobalAuto("100"));
        NSObject str3 = (NSObject) klass.Call("stringWithUTF8String:", Marshal.StringToHGlobalAuto("foo"));

        Stopwatch timer = Stopwatch.StartNew();
        for (int i = 0; i < 10000; ++i)
        {
            str3.Call("hasPrefix:", str1);
        }
        Console.WriteLine("{0}	{1:0.0} secs", new Native(str3, new Selector("hasPrefix:")), timer.ElapsedMilliseconds/1000.0);

        timer = Stopwatch.StartNew();
        for (int i = 0; i < 10000; ++i)
        {
            str2.Call("intValue");
        }
        Console.WriteLine("{0}	{1:0.0} secs", new Native(str2, new Selector("intValue")), timer.ElapsedMilliseconds/1000.0);

        timer = Stopwatch.StartNew();
        for (int i = 0; i < 10000; ++i)
        {
            str1.Call("uppercaseString");
        }
        Console.WriteLine("{0}	{1:0.0} secs", new Native(str1, new Selector("uppercaseString")), timer.ElapsedMilliseconds/1000.0);

        Native native = new Native(str1, new Selector("uppercaseString"));
        timer = Stopwatch.StartNew();
        for (int i = 0; i < 10000; ++i)
        {
            native.Invoke();
        }
        Console.WriteLine("Native {0}	{1:0.0} secs", native, timer.ElapsedMilliseconds/1000.0);

        string s = "hello world";
        timer = Stopwatch.StartNew();
        for (int i = 0; i < 10000; ++i)
        {
            s.ToUpper();
        }
        Console.WriteLine("Managed ToUpper: {0:0.0} secs", timer.ElapsedMilliseconds/1000.0);
    }
开发者ID:afrog33k,项目名称:mobjc,代码行数:44,代码来源:TimingTest.cs

示例9: BoolWithTo

    public void BoolWithTo()
    {
        Class klass = new Class("NSNumber");

        NSObject num = klass.Call("alloc").To<NSObject>();
        num = num.Call("initWithBool:", (sbyte) 1).To<NSObject>();

        sbyte result = num.Call("boolValue").To<sbyte>();
        Assert.AreEqual(1, result);
    }
开发者ID:afrog33k,项目名称:mobjc,代码行数:10,代码来源:ReturnTests.cs

示例10: ChainedCallTest

    public void ChainedCallTest()
    {
        NSObject pool = new NSObject(NSObject.AllocAndInitInstance("NSAutoreleasePool"));

        Class nsString = new Class("NSMutableString");
        NSObject str = (NSObject) nsString.Call("alloc").Call("initWithUTF8String:", Marshal.StringToHGlobalAuto("chained!"));

        string result = Marshal.PtrToStringAuto((IntPtr) str.Call("UTF8String"));
        Assert.AreEqual("chained!", result);

        pool.release();
    }
开发者ID:afrog33k,项目名称:mobjc,代码行数:12,代码来源:InstanceTests.cs

示例11: Block

    public void Block()
    {
        if (ExtendedBlock.HasBlocks())
        {
            NSObject s = new Class("NSString").Call("stringWithUTF8String:", Marshal.StringToHGlobalAuto("hello\nthere")).To<NSObject>();

            var lines = new List<string>();
            Enumerator e = (IntPtr context, IntPtr line, ref byte stop) =>
            {
                lines.Add(new NSObject(line).Call("description").ToString());
            };
            var block = new ExtendedBlock(e);
            s.Call("enumerateLinesUsingBlock:", block);

            Assert.AreEqual(2, lines.Count);
            Assert.AreEqual("hello", lines[0]);
            Assert.AreEqual("there", lines[1]);
        }
    }
开发者ID:afrog33k,项目名称:mobjc,代码行数:19,代码来源:ArgTests.cs

示例12: RefCount1Test

    public void RefCount1Test()
    {
        NSObject pool = new NSObject(NSObject.AllocAndInitInstance("NSAutoreleasePool"));

        // If we use alloc the object will have a ref count of one.
        NSObject instance = (NSObject) new Class("NSHashTable").Call("alloc").Call("init");
        Assert.AreEqual(1L, instance.retainCount());

        // Classes always have a very high retain count (because they
        // are not supposed to go away).
        Class nsSignature = new Class("NSMethodSignature");
        Assert.IsTrue(nsSignature.retainCount() > 1000);

        // If alloc, new, or copy aren't used then the pool owns the object.
        Class nsString = new Class("NSString");
        NSObject str = (NSObject) nsString.Call("stringWithUTF8String:", Marshal.StringToHGlobalAuto("hello"));
        Assert.AreEqual(1L, str.retainCount());

        // We can have two managed instances on the same native instance
        // and the ref count doesn't change.
        NSObject copy = new NSObject((IntPtr) instance);
        Assert.AreEqual(1L, copy.retainCount());

        // If we send a message to an object its retain count doesn't change.
        instance.Call("description");
        Assert.AreEqual(1L, instance.retainCount());

        pool.release();

        // Verify our counts after we empty the release pool.
        Assert.AreEqual(1L, instance.retainCount());
        Assert.AreEqual(1L, copy.retainCount());
    }
开发者ID:afrog33k,项目名称:mobjc,代码行数:33,代码来源:InstanceTests.cs

示例13: DerivedWithTo

    public void DerivedWithTo()
    {
        NSObject o = new Class("Subclass1").Call("alloc").Call("initValue").To<NSObject>();

        NSObject p = o.Call("Clone").To<NSObject>();
        Assert.IsTrue((IntPtr) o != (IntPtr) p);
        Assert.AreEqual(100, (int) p.Call("getValue"));

        Subclass1 q = o.Call("Clone").To<Subclass1>();
        Assert.IsTrue((IntPtr) o != (IntPtr) q);
        Assert.AreEqual(100, q.GetValue());
    }
开发者ID:afrog33k,项目名称:mobjc,代码行数:12,代码来源:ReturnTests.cs

示例14: IntArg

    public void IntArg()
    {
        NSObject pool = new NSObject(NSObject.AllocAndInitInstance("NSAutoreleasePool"));
        long bytes = DoGetMemory();

        Class nsString = new Class("NSString");
        NSObject str = (NSObject) nsString.Call("stringWithUTF8String:", Marshal.StringToHGlobalAuto("hello world"));

        for (int j = 1; j < 100; ++j)
        {
            for (int i = 0; i < NumIterations/100; ++i)
            {
                str.Call("characterAtIndex:", 2);
            }
            GC.Collect();
        }

        pool.release();
        GC.Collect();
        GC.WaitForPendingFinalizers();

        long delta = DoGetMemory() - bytes;
        if (delta/NumIterations > 4)
            Assert.Fail("IntArg used {0}K of memory ({1} bytes per iteration)!", delta/1024, delta/NumIterations);
    }
开发者ID:afrog33k,项目名称:mobjc,代码行数:25,代码来源:MemoryTests.cs

示例15: DoCreateStr

 private NSObject DoCreateStr(string s)
 {
     Class nsString = new Class("NSMutableString");
     NSObject str = (NSObject) nsString.Call("alloc");
     return (NSObject) str.Call("initWithUTF8String:", Marshal.StringToHGlobalAuto(s));
 }
开发者ID:afrog33k,项目名称:mobjc,代码行数:6,代码来源:ManagedTypes.cs


注:本文中的Class.Call方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。