Tag: 传递引用

如何将C#List中创建的对象的地址传递给C ++ DLL?

我一直在谷歌寻找我的问题的一些答案,但不太明白我发现了什么。 在使用System.IO读取一些文本文件后,我有一些在C#List中创建和存储的对象。 之后,我想将每个对象的引用(使用const指针)发送到C ++ dll中的内部类,以便它可以使用它们来计算某些算法。 以下是我正在做的一些简单示例(不是实际代码): C#类: [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] public class SimpleClass { [MarshalAs(UnmanagedType.LPStr)] public string Name; public float Length; } 与相应的C结构: struct SimpleClass { const char* Name; float Length; }; 存储在 List ItemList; 解析一些文本文件后。 然后调用以下dll函数: C#: [DllImport(“SimpleDLL”)] public static extern void AddSimpleReference(SimpleClass inSimple); C: void AddSimpleReference(const SimpleClass* inSimple) { g_Vector.push_back(inSimple); // g_Vector […]

使用“ref”键将“引用类型”作为参数传递给方法是否有意义?

可能重复: C#:参考类型变量的“ref”有什么用? 嗨, 使用“ref”键将“引用类型”作为参数传递给方法是否有意义? 或者它只是废话,因为它已经是引用类型但不是值类型? 谢谢!

C#对象修改

我认为这真的是一个非常蹩脚的问题,但我想我会得到答案比通过谷歌搜索自己:) 假设我有一些带有构造函数的类: public class TestClass { private readonly IRepository repository; public TestClass(IRepository repository) { this.repository = repository; // Here is the non-obvious line – it invokes some // method, which changes the internal contents of // the repository object. repository.AddSomething(…); } } 现在: IRepository sqlRepository = new SqlRepository(…); TestClass testClass = new TestClass(sqlRepository); 1)我不擅长传递C#价值/参考 – […]

通过引用传递:多个接口的子节点

将实现多个接口的对象传递给只需要其中一个接口的函数时,我遇到了构建错误。 我正在开发一个通讯包。 该软件包有一个Receiver类和一个Sender类。 Receiver类使用通知接口Receive_Notifier(函数对象)来处理通知。 同样,Sender类使用通知接口Send_Notifier来处理通知。 interface Send_Notifier { void sending_text(string text); } interface Receive_Notifier { void raw_text_received(string raw_text); } class Sender { Send_Notifier m_notifier = null; public Sender(ref Send_Notifier notifier) { m_notifier = notifier; } public void send(string text) { m_notifier.sending_text(text); return; } } class Receiver { Receive_Notifier m_notifier = null; public Sender(ref Receive_Notifier notifier) […]

通过VB.NET和C#中的Ref参数

我有问题相关的传递参数byRef,我有基于VB.NET的类库,其中一些函数用byref参数类型定义。 这些参数是父类对象,当我尝试调用此函数并在byref参数中传递子类对象时,它在VB.NET中工作但我无法在C#中执行相同的操作 以下是我正在尝试的测试代码 Public Class Father Private _Cast As String Public Property Cast() As String Get Return _Cast End Get Set(ByVal value As String) _Cast = value End Set End Property End Class Public Class Son Inherits Father Private _MyName As String Public Property Myname() As String Get Return _MyName End Get Set(ByVal value As […]

IList是否通过了价值?

除非在参数上使用ref或out关键字,否则将值类型参数传递给c#中的函数。 但这也适用于参考类型吗? 具体来说,我有一个函数,它采用IList 。 传递给我的函数的列表是否是包含其包含对象的副本的列表的副本? 或者对列表的修改是否也适用于呼叫者? 如果是这样的话 – 我可以通过一种聪明的方式传递副本吗? public void SomeFunction() { IList list = new List(); list.Add(new Foo()); DoSomethingWithCopyOfTheList(list); .. } public void DoSomethingWithCopyOfTheList(IList list) { // Do something }

C#通过引用传递属性

无论如何通过引用传递Object的属性? 我知道我可以传递整个对象,但是我想指定要设置的对象的属性并检查它的类型,以便我知道如何解析。 我应该采取另一种方法(无论如何我都无法改变原始物体)? public class Foo{ public Foo(){} public int Age { get; set; } } private void setFromQueryString(object aProperty, String queryString, HttpContext context) { //here I want to handle pulling the values out of //the query string and parsing them or setting them //to null or empty string… String valueString = context.Request.QueryString[queryString].ToString(); //I need […]

C#4.0’dynamic’不设置ref / out参数

我正在尝试使用DynamicObject 。 我尝试做的一件事是设置ref / out参数的值,如下面的代码所示。 但是,我无法正确设置Main()的i和j值(即使它们在TryInvokeMember()中设置正确)。 有没有人知道如何使用ref / out参数调用DynamicObject对象,并能够检索方法中设置的值? class Program { static void Main(string[] args) { dynamic proxy = new Proxy(new Target()); int i = 10; int j = 20; proxy.Wrap(ref i, ref j); Console.WriteLine(i + “:” + j); // Print “10:20” while expect “20:10” } } class Proxy : DynamicObject { private readonly […]

是否可以将属性作为“out”或“ref”参数传递?

我可以将属性作为“out”或“ref”参数传递,如果没有那么为什么不呢? 例如 Person p = new Person(); 。 。 。 public void Test(out p.Name);