区分由vale或ref传递的数据。 [英] Distinguishing between data passed by vale or by ref.

查看:51
本文介绍了区分由vale或ref传递的数据。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我对一个奇怪的结果感到有些困惑,这个结果是没有造成

错误,但也不会给出结果我会期待。


我的目标是将数据输出到文本文件。


如果数据是按值传递的,则输出名称和数据。


如果数据是由ref引用的,只是输出名称作为

数据的引用,因为数据先前已经在文件中输出了bean。


例如,这段代码应该按值或ref接受字符串,并且

输出为true或false,具体取决于女巫。

//代码

类TestClass

{

public void Test(字符串Obj)

{

System.Type Type = Obj.GetType();

if(Type.IsByRef == true)

{System.Windows。 Forms.MessageBox.Show(QUOT;真"); }

else {System.Windows.Forms.MessageBox.Show(" False"); }

}

public void Test(ref string Obj)

{

System.Type Type = Obj。 GetType();

if(Type.IsByRef == true)

{System.Windows.Forms.MessageBox.Show(" True"); }

else {System.Windows.Forms.MessageBox.Show(" False"); }

}

}

//结束


用值调用然后参考,例如


//代码

string theString =" Data";

TestClass TestClass = new TestClass();

TestClass.Test(theString);

TestClass.Test(ref theString);

// END


世界期望它显示假然后是真。


但是被称为假总是输出,我无法理解为什么。

我使用了错误的方法或做了别的错误。


提前感谢您的任何建议。

Hi all,

I am slightly confused about an odd result that douse not create an
error, but also douse not give the result I would expect.

My aim was to output data to a text file.

If the data was passed by value, output the name and the data.

If the data was by ref to output just the name as reference to the
data because the data has already bean output previously in the file.

As an example, this code should accept a string by value or ref and
output true or false depending on witch.

// CODE
class TestClass
{
public void Test(string Obj)
{
System.Type Type = Obj.GetType();
if (Type.IsByRef == true)
{ System.Windows.Forms.MessageBox.Show("True"); }
else { System.Windows.Forms.MessageBox.Show("False"); }
}
public void Test(ref string Obj)
{
System.Type Type = Obj.GetType();
if (Type.IsByRef == true)
{ System.Windows.Forms.MessageBox.Show("True"); }
else { System.Windows.Forms.MessageBox.Show("False"); }
}
}
// END

When called with a value then a ref, e.g.

// CODE
string theString = "Data";
TestClass TestClass = new TestClass();
TestClass.Test(theString);
TestClass.Test(ref theString);
// END

I world expect it to display "False" then "True".

However called "false" is always output and I can not understand why.
Am I using the wrong approach or doing something else wrong.

Thank you in advance for any suggestions.

推荐答案

10月5日上午6:30,Jay Dee< first ... @ gmail.comwrote:
On Oct 5, 6:30 am, Jay Dee <first...@gmail.comwrote:

我对一个奇怪的结果感到有些困惑,这个结果不会造成

错误,但也不能给出我期望的结果。


我的目标是将数据输出到文本文件。


如果数据是按值传递的,则输出名称和数据。


如果数据是通过ref输出的名称作为对

数据的引用,因为数据先前已经在文件中输出了bean。


例如,这段代码应该按值或ref接受字符串,并且

输出真或假,具体取决于女巫。
I am slightly confused about an odd result that douse not create an
error, but also douse not give the result I would expect.

My aim was to output data to a text file.

If the data was passed by value, output the name and the data.

If the data was by ref to output just the name as reference to the
data because the data has already bean output previously in the file.

As an example, this code should accept a string by value or ref and
output true or false depending on witch.



您正在查看对象的类型,并且无论*参数*是否已通过,都将是相同的

通过引用或按价值。

类型的对象根据参数的传递方式不变,

这就是为什么你在两个方面得到相同的答案。


这是你使用ref的唯一原因吗?或不?如果是这样,我只会

传递一个布尔值。


无论如何,这里有一些代码可以满足您的需求,通过检查

反射方法:


使用System;

使用System.Reflection;


公共课测试

{

static void Main()

{

string x =" ;" ;;

ByVal(x);

ByRef(ref x);

}


static void ByVal(string x)

{

ShowFirstParameterType(MethodBase.GetCurrentMethod());

}


static void ByRef(ref string x)

{

ShowFirstParameterType(MethodBase.GetCurrentMethod());

}


static void ShowFirstParameterType(MethodBase方法)

{

ParameterInfo [] pi = method.GetParameters();

Console.WriteLine(pi [0] .ParameterType.IsByRef);

}

}


Jon

You''re looking at the type of the object, and that will be the same
whether the *parameter* has been passed by reference or by value. The
type of the object is unchanged by how the parameter has been passed,
which is why you''re getting the same answer in both ways.

Is this the only reason why you''re using "ref" or not? If so, I''d just
pass a boolean instead.

Anyway, here''s some code which does what you want it to, by examining
the method with reflection:

using System;
using System.Reflection;

public class Test
{
static void Main()
{
string x = "";
ByVal(x);
ByRef(ref x);
}

static void ByVal(string x)
{
ShowFirstParameterType(MethodBase.GetCurrentMethod ());
}

static void ByRef(ref string x)
{
ShowFirstParameterType(MethodBase.GetCurrentMethod ());
}

static void ShowFirstParameterType(MethodBase method)
{
ParameterInfo[] pi = method.GetParameters();
Console.WriteLine(pi[0].ParameterType.IsByRef);
}
}

Jon


很抱歉没有很好地解释这个问题,并且感谢您的

答案。


启动项目的原因是模型构建软件

就像Blender和AC3D一样,虽然有用并不是专门为

构建的DirectX格式,但每个都有局限性,我虽然它会是

a挑战构建我自己的。


我对编程很新,并且想要学习存储和操作数据的基本知识。


罪恶二进制看起来有点复杂我开始使用文本文件。


DirectX格式将数据保存为.xt文件并且看起来很好

起点所以我构建了一个DLL,它从.x

文件中读取数据,并填充可用于显示3D模型的数组。


那么当它可以将数据保存回文件时我就会卡住

,因为它重复数据而不是重新参考它是

它。


例如


材料1

{

1.0; 1.0; 1.0; 1.0 ;;

64.0;

0.5; 0.5; 0.5 ;;

0.0; 0.0; 0.0 ;;

TextureFileName {" Image.png" ;; }

}


Mesh MyMesh

{

MeshMaterialList

{

1;

{Material1}

}

}


输出为:


材料1

{

1.0; 1.0; 1.0; 1.0 ;;

64.0;

0.5; 0.5; 0.5 ;;

0.0; 0.0; 0.0 ;;

TextureFileName {" Image.png" ;; }

}


Mesh MyMesh

{

MeshMaterialList

{

1;

材料1

{

1.0; 1.0; 1.0; 1.0 ;;

64.0;

0.5; 0.5; 0.5 ;;

0.0; 0.0; 0.0 ;;

TextureFileName {" Image.png" ;; }

}

}

}

System.Reflection为我解答了问题。


目前我的所有程序都会读取.x文件中的数据

用DirectX显示它并将其保存回文件但是我希望

添加修改3d模型的能力或者从头开始构建一个

它。


然后可能会查看将数据保存在二元格式。


谢谢大家的帮助。


Jay Dee。

Sorry for not explaining the problem very well and thank you for the
answers.

The reason for starting the project was that Model building software
like Blender and AC3D, though useful were not built specifically for
the DirectX format and each have limitations and I though it would be
a challenge to build my own.

I am fairly new to programming and wanted to learn the basics of
storing and manipulating data.

Sins binary looked a bit to complicated I started with text files.

The DirectX format saves data as txt to a .x file and looked a good
place to start so I have built a DLL that reads the data from a .x
file and populates arrays that can be used to display 3D models.

Then when it can to saving the data back the file I became stuck
because it was repeating the data rather than making a reference to
it.

E.g.

Material Material1
{
1.0; 1.0; 1.0; 1.0;;
64.0;
0.5; 0.5; 0.5;;
0.0; 0.0; 0.0;;
TextureFileName { "Image.png"; }
}

Mesh MyMesh
{
MeshMaterialList
{
1;
{ Material1 }
}
}

Was being output as:

Material Material1
{
1.0; 1.0; 1.0; 1.0;;
64.0;
0.5; 0.5; 0.5;;
0.0; 0.0; 0.0;;
TextureFileName { "Image.png"; }
}

Mesh MyMesh
{
MeshMaterialList
{
1;
Material Material1
{
1.0; 1.0; 1.0; 1.0;;
64.0;
0.5; 0.5; 0.5;;
0.0; 0.0; 0.0;;
TextureFileName { "Image.png"; }
}
}
}
System.Reflection has answers the problem for me.

Currently all my program will do is read the data from the .x file
display it with DirectX and save it back to file but I am hoping to
add the ability to modify the 3d model or build one from scratch with
it.

Then maybe look in to saving the data in the binary format.

Thank you all for the help.

Jay Dee.


Jay Dee写道:
Jay Dee wrote:

[...]

然后它可以将数据保存回文件我被卡住了

,因为它重复数据而不是引用

它。


例如


[...]

Mesh MyMesh

{

MeshMaterialList

{

1;

{Material1}

}

}


输出为:


[...]

网格MyMesh

{

MeshMaterialList

{

1;

Material Material1

{

1.0; 1.0; 1.0; 1.0 ;;

64.0;

0.5; 0.5; 0.5 ;;

0.0; 0.0; 0.0 ;;

TextureFileName {" Image.png" ;; }

}

}

}


System.Reflection为我解答了问题。
[...]
Then when it can to saving the data back the file I became stuck
because it was repeating the data rather than making a reference to
it.

E.g.

[...]
Mesh MyMesh
{
MeshMaterialList
{
1;
{ Material1 }
}
}

Was being output as:

[...]
Mesh MyMesh
{
MeshMaterialList
{
1;
Material Material1
{
1.0; 1.0; 1.0; 1.0;;
64.0;
0.5; 0.5; 0.5;;
0.0; 0.0; 0.0;;
TextureFileName { "Image.png"; }
}
}
}
System.Reflection has answers the problem for me.



我怀疑,你的问题绝对与反射无关。

你可能在Reflection中使用了一些功能来解决它​​,但是

根本没有必要。你只需要让代码更加聪明地了解它如何写出数据。


Pete

As I suspected, your problem has absolutely zero to do with Reflection.
You may have used some feature in Reflection to address it, but
there''s no need to at all. You just need to make the code more
intelligent about how it writes the data out.

Pete


这篇关于区分由vale或ref传递的数据。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆