天道酬勤,学无止境

reference-parameters

Why can't I retrieve the value for parameters of type out or ref using Type.InvokeMember?

A long title, but I wanted it to be specific. The title is really the question. Even though the method that InvokeMember is calling has an out parameter and is assigning a value to to that parameter I can't grab that value. Here is the code I was initially using: string parameter = ""; int result = Convert.ToInt32(typeof(Ability).InvokeMember(selectedMove, BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static, null, null, new object[] { parameter })); I changed it this, which now makes it work as intended but I don't know why: object[] args = new object[1]; //necessary to

2021-05-02 01:54:43    分类:问答    c#   reflection   reference-parameters

如何在C ++中使用参考参数?(How do I use Reference Parameters in C++?)

问题 我试图了解如何使用参考参数。 我的文字中有几个示例,但是对于我来说,它们太复杂了,无法理解为什么以及如何使用它们。 您想如何以及为什么要使用参考? 如果不将参数设为引用,而是忽略了& ,将会发生什么? 例如,这些功能之间有什么区别: int doSomething(int& a, int& b); int doSomething(int a, int b); 我知道使用引用变量来更改formal-> reference,然后允许双向交换参数。 但是,这是我所了解的程度,一个更具体的示例将大有帮助。 回答1 将引用视为别名。 当您在引用上调用某些东西时,您实际上是在引用所引用的对象上调用它。 int i; int& j = i; // j is an alias to i j = 5; // same as i = 5 关于功能,请考虑: void foo(int i) { i = 5; } 在上面, int i是一个值,并且传递的参数由value传递。 这意味着如果我们说: int x = 2; foo(x); i将是x的副本。 因此将i设置为5对x无效,因为它是x的副本被更改。 但是,如果我们给i一个参考: void foo(int& i) // i is an alias for a variable { i = 5; } 然后说foo(x)不再复制x ; i是x 。

2021-04-10 06:34:32    分类:技术分享    c++   reference-parameters

C# property and ref parameter, why no sugar?

I just ran across this error message while working in C# A property or indexer may not be passed as an out or ref parameter I known what caused this and did the quick solution of creating a local variable of the correct type, calling the function with it as the out/ref parameter and then assigning it back to the property: RefFn(ref obj.prop); turns into { var t = obj.prop; RefFn(ref t); obj.prop = t; } Clearly this would fail if the property doesn't support get and set in the current context. Why doesn't C# just do that for me? The only cases where I can think of where this might cause

2021-03-28 17:04:10    分类:问答    c#   properties   syntactic-sugar   reference-parameters

How do I use Reference Parameters in C++?

I am trying to understand how to use reference parameters. There are several examples in my text, however they are too complicated for me to understand why and how to use them. How and why would you want to use a reference? What would happen if you didn't make the parameter a reference, but instead left the & off? For example, what's the difference between these functions: int doSomething(int& a, int& b); int doSomething(int a, int b); I understand that reference variables are used in order to change a formal->reference, which then allows a two-way exchange of parameters. However, that is the

2021-03-26 06:32:05    分类:问答    c++   reference-parameters