我有一些类需要可靠性来将所有公共属性从一个对象复制到另一个对象.
每个类都有一组可能与其他类不同的公共属性.
例:
class Base
{
// Common properties/methods...
public void Copy<T>(T data) where T : Base
{
// ...
}
}
class D1 : Base
{
public int ID
{
get;
set;
}
}
class D2 : Base
{
public string Name
{
get;
set;
}
}
Run Code Online (Sandbox Code Playgroud)
通过谷歌搜索我已经阅读了这些方法:
所有这些都非常复杂或非常慢或有时两者兼而有之.
我错过了什么吗?有没有其他方法来访问原始this指针?
编辑:
我会clerify.
T是调用类的类型.例如,如果它被D1调用,则T将始终为D1.
通用的原因是我不知道T是什么.
我错过了什么吗?
我应该Base data用作参数吗?
这适用于.NET.我正在寻找一个空间映射应用程序.内存中将同时存在多个多边形(大约30-50个多边形).每个polgon都有一系列LatLong点.每个多边形的集合范围可以从10到200.但是,有很多计算将使用这些点来完成,这就是为什么(为了性能)我想让LatLong成为一个结构.但是我厌倦了大量的LatLong会在内存中.对此的任何见解将不胜感激.回顾一下:我想知道我是否将LatLong作为一个结构,因为我想要计算性能,或者一个类,因为将在内存中的latLongs数量为1.
正如许多书中所提到的,与C++结构和类的区别在于访问控制描述符.因此,我想知道以下陈述是否正确:
C中的struct是unboxed:struct中的成员明显位于分配struct的位置.但是C++中的struct是类似盒子的类型:members/headers位于其他地方,并且分配struct的地方包含指向members/headers的指针.
这种理解对吗?
是否可以在C++中创建一个未装箱的类型,它还包含实例方法?
在bodyis的简单移动脚本中gameObject.GetComponent<Rigidbody2D> (),此代码应用移动
body.velocity = new Vector2 (10 * dir, body.velocity.y);
Run Code Online (Sandbox Code Playgroud)
然而,在这一个没有运动(也没有错误)出现
body.velocity.Set (10 * dir, body.velocity.y);
Run Code Online (Sandbox Code Playgroud)
我在Unity 文档中没有看到任何解释。你能解释为什么body.velocity.x在第二种情况下仍然为零吗?
我已经自定义了一个用于链接调用的 C# 类来添加我的样式,但是当我第一次创建样式类的实例然后使用链接调用时,它最终只显示第一个添加的样式。
这是代码:
我的样式生成器:
namespace TestStyleBuilder
{
public struct MyStyleBuilder
{
private string stringBuffer;
public MyStyleBuilder AddLineColor(string value) => this.AddStyle("color", value);
public MyStyleBuilder AddLineDash(string value) => this.AddStyle("dash", value);
public MyStyleBuilder AddStyle(string prop, string value)
{
var style = $"{prop}:{value};";
stringBuffer += style;
return this;
}
public override string ToString()
{
return stringBuffer != null ? stringBuffer.Trim() : string.Empty;
}
}
}
Run Code Online (Sandbox Code Playgroud)
测试:
using TestStyleBuilder;
// Output:
// color:color;dash:dash;
MyStyleBuilder builder1 = new MyStyleBuilder().AddLineColor("color").AddLineDash("dash");
Console.WriteLine(builder1.ToString());
// Output:
// color:color;
MyStyleBuilder …Run Code Online (Sandbox Code Playgroud) 添加到数组中我遇到了麻烦.我创建了一个名为Products的类:
public struct newProducts
{
public string productBrand;
public string productType;
public string productName;
public string productFlavour;
public int productSize;
}
//Create an array of type newProducts
newProducts[] productList = new productList[];
Run Code Online (Sandbox Code Playgroud)
而且我创建了一个函数:
public newProducts AddProduct(string brand, string type, string name, string flavour, int size)
{
//I don't know what to do here..
return productList;
}
Run Code Online (Sandbox Code Playgroud)
我想要做的是将品牌,类型,名称,风味和大小值附加并存储到数组中
基本上我第一次调用这个函数时,我会输入这些值并将其存储到索引0,在第二次调用时它会将它们添加到索引1.
这可能吗?
当我不知道元素的确切数量时,我有一个关于在C#中创建数组的问题.我发现了很多类似的问题,比如这个问题.建议使用List而不是Array.在那个例子和其他所有例子中,我看到它实例化了一个'字符串列表'.我可以用结构做同样的事情吗?
例如,我有以下用于存储故障信息的结构,但我不知道在测试开始时会发生多少次故障.它可能是0,可能是数百.
public struct EraseFailures
{
public string timestamp;
public string bus;
public string channel;
public string die;
public string block;
}
Run Code Online (Sandbox Code Playgroud)
那么我也可以创建这些列表吗?
List<EraseFailures> eFails = new List<EraseFailures>();
Run Code Online (Sandbox Code Playgroud)
它编译得很好,但我想确保这是最好的方法.如果是,将值添加到我的列表的语法是什么?因为以下似乎不正确......
eFails.bus.Add("bus0");
Run Code Online (Sandbox Code Playgroud)
好像我可能要创建一个包含我想要的值的新结构,然后将其添加到List中.
我在某处读到我不应该在游戏引擎的 Update() 方法中创建任何新对象实例,因为这会导致唤醒垃圾收集器并降低性能,但有时我在一些教程中看到他们使用 new 关键字unity Update() 方法!这个可以吗?团结会以某种方式处理这个问题,还是不?
我想创建一个Vector3类型的对象,引用我的播放器的位置,以便我可以在我的脚本中使用较短的名称.但我没有这样做.似乎新对象指向其他地址.
我写了一些像这样的代码:
//PlayerController.cs
vector3 playerPos = transform.position;
Debug.Log(System.Object.ReferenceEquals(playerPos,transform.position));
Run Code Online (Sandbox Code Playgroud)
输出是
假
造成这种情况的原因是什么?如何正确引用我的球员位置?
有没有办法以这种方式更改扩展方法内的 Unity Vector3 结构值?
Vector3 myVector = new Vector(3.5f, 0.7f, 2f);
myVector.Floor(); // it should be (3f, 0f, 2f) instead it's (3.5f, 0.7f, 2f) still
Run Code Online (Sandbox Code Playgroud)
它只能以这种方式工作:
myVector = myVector.Floor(); // now it's (3f, 0f, 2f)
Run Code Online (Sandbox Code Playgroud)
扩展代码:
public static void Floor(this Vector3 vector)
{
vector.Set(Mathf.Floor(vector.x), Mathf.Floor(vector.y), Mathf.Floor(vector.z));
}
Run Code Online (Sandbox Code Playgroud)
或者
public static void Floor(this Vector3 vector)
{
vector.x = Mathf.Floor(vector.x);
vector.y = Mathf.Floor(vector.y);
vector.z = Mathf.Floor(vector.z);
}
Run Code Online (Sandbox Code Playgroud) A lot of times the answer is merely when I want things to be allocated on a stack instead of the heap.. assuming I dont know what the stack and the heap are (and please dont try to explain it here), when exactly should I be using structs instead of classes?
Here is the answer I've been giving out, but please tell me if I'm wrong or falling short of a better answer:
I create structs usually when I have …
我在使用"Form1_Load"中声明的数组时遇到问题.我的目标是能够为数组赋值,然后从其中的某些值中减去.
该数组是一个结构数组.我想我必须在其他地方公开宣布这个数组.现在我想尝试在"Form1_load"中完成大部分操作.
我想要实现的是,如果用户点击图片,它应该更新剩余的项目数(从20开始)并将总数添加到标签.他们可以点击5个不同的图片,这是阵列需要的地方.
结构:
struct Drink
{
public string name;
public int cost;
public int numberOfDrinks = 20;
}
Run Code Online (Sandbox Code Playgroud)
加载事件:
private void Form1_Load(object sender, EventArgs e)
{
const int SIZE = 5;
Drink[] drink = new Drink[SIZE];
}
Run Code Online (Sandbox Code Playgroud)
这是一个如果点击图片会发生什么的例子:
private void picCola_Click(object sender, EventArgs e)
{
drink[0].cost = 1.5;
drink[0].name = "";
}
Run Code Online (Sandbox Code Playgroud)
c# ×11
struct ×4
arrays ×3
.net ×2
unity5 ×2
c ×1
c++ ×1
chained ×1
java ×1
list ×1
naming ×1
performance ×1
properties ×1
reference ×1
shallow-copy ×1