这就是我的意思:
我需要能够替换这个丑陋的C#代码:
if (attribute.Name == "Name") machinePool.Name = attribute.Value;
else if (attribute.Name == "Capabilities") machinePool.Capabilities = attribute.Value;
else if (attribute.Name == "FillFactor") machinePool.FillFactor = attribute.Value;
Run Code Online (Sandbox Code Playgroud)
进入这样的事情:
machinePool.ConvertStringToObjectField(attribute.Name) = attribute.Value;
Run Code Online (Sandbox Code Playgroud)
没有ConvertStringToObjectField()方法,但我希望有这样的东西,如果可能的话.我可以访问machinePool对象类代码,所以我可以添加必要的代码,但我不确定它可能是什么代码,或者甚至可以在C#中进行.
我有非常简单的Bitwise运算符方法,我想这样使用:
myInt.SetBit(int k, bool set)
Run Code Online (Sandbox Code Playgroud)
所以它将索引'k'的位改为值'set'(0或1)我首先想到这样做:
public static void SetBit(this int A, int k, bool set) {
if (set) A |= 1 << k;
else A &= ~(1 << k);
}
Run Code Online (Sandbox Code Playgroud)
但当然这只会改变变量'A'的内部值,而不是原始变量,因为整数不是引用类型.
我不能将'ref'和'this'一起使用,所以我不知道如何把它变成参考参数.
我已经有类似Int数组的方法,但这些方法工作正常,因为数组是引用类型.
我正在寻找一种方法来为单个整数保留这个方便的语法,如果有的话.
我有一个非常奇怪的问题。在我的一篇论文中,根本没有打印对章节的引用。我用 pdflatex 进行编译,它的工作没有任何抱怨,并且它会输出 pdf,但没有任何对部分的引用。
我只是在一个部分之后使用 \label{sec:mysec} ,并在文本中的任何地方使用 \ref{sec:mysec} ,这是典型的用法。我已经使用它很多年了,但是在这个特定的文档中,必须与阻止乳胶显示引用的东西进行交互。
如果有帮助,这些是我要导入的包:
\documentclass[a4paper,man,natbib,floatsintext]{apa6}
\usepackage[utf8]{inputenc}
\usepackage{multirow}
\usepackage{graphicx}
\usepackage{rotating}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{color}
\usepackage{array}
Run Code Online (Sandbox Code Playgroud)
我不知道它是从哪里来的。任何提示都将非常受欢迎。
多谢!
我试图在react v16.4.0中通过flowtype定义ref的类型,
但是我无法解决它,所以请让我知道如何定义它。
这些是示例代码。
我想知道如何定义道具类型的引用。
export default class ParentComponent extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
....
this.listRef = createRef();
}
render() {
return (
<ChildComponent
listRef={this.listRef}
/>
);
}
}
Run Code Online (Sandbox Code Playgroud)
type Props = {
listRef: Ref<>, // how to define here?
};
const ChildComponent = (props: Props) => {
<div>
<ul ref={props.listRef}>
...
</ul>
...
</div>
}
Run Code Online (Sandbox Code Playgroud)
"react": "^16.4.0",
"flow-bin": "^0.73.0",
Run Code Online (Sandbox Code Playgroud) 鉴于,
private object _x;
private object LoadAndSet(ref object x) {
// lock established over read and update
lock (somePrivateObjectNotUsedElsewhereThatIsIrrelvantToTheQuestion) {
if (x == null)
x = Load();
return x;
}
}
Run Code Online (Sandbox Code Playgroud)
调用为,
public object Load() {
return LoadAndSet(ref _x);
}
Run Code Online (Sandbox Code Playgroud)
_xlock也就是说,第一个代码等价于下面直接使用字段的地方吗?(赋值直接发生,而不是通过ref参数。)
private object _x;
private object LoadAndSetFieldDirectly() {
// lock established over read and update
lock (somePrivateObjectNotUsedElsewhereThatIsIrrelvantToTheQuestion) {
if (_x == null)
_x = Load();
return _x;
}
} …Run Code Online (Sandbox Code Playgroud) 鉴于最新版本的C#7中新的ref locals和ref return功能,这个问题是新近相关的:
随着C#中托管变量或"内部" - 指针变量的突出和广泛使用,有时您可能需要为这样的指针恢复相应的包含 Pinnable GC对象.例如,如果要传递一个指向类型数组元素的托管指针T,则可能需要数组引用T[]本身才能调用(例如)Array.Copy(...).
所以,从托管代码,是否有任何合理合法以回收含GC对象句柄方式,给出以下两种流行内部的/管理的指针(
ref,out,in)使用:
- 指向GC对象实例中(
struct或class)字段的内部指针;- 管理指向Array 的(
struct或class)元素T的指针T[].
在.NET内部,GC似乎使用以下函数:/ coreclr/master/src/gc/gc.cpp
#ifdef INTERIOR_POINTERS
// will find all heap objects (large and small)
uint8_t* gc_heap::find_object (uint8_t* interior, uint8_t* low)
{
....
Run Code Online (Sandbox Code Playgroud)
此代码遍历已知的GC堆,检查指定的内部指针是否落在已知GC对象分配的范围内.显然,这种方法不容易从最终用户托管代码中访问,而且据我所知,如果没有GC正在进行中,则可能甚至不相关.
我还查看了新的Span<T>和System.Memory库,但是找不到一系列可以恢复(例如)数组句柄的操作,如果你没有从第一个位置提供它(那时包含的句柄在那些中被squirleled)各种结构).如果_pinnable是可选的(例如Span<T> …
我将 ReactJS 与 NextJS 一起使用。当我尝试设置 a 时ref,我的控制台返回给我undefined,这怎么可能?如何解决这个困难呢?我尝试在网上阅读一些建议,但没有成功。
这是我的片段:
componentDidMount() {
this.myRef = React.createRef();
window.addEventListener('scroll', this.handleScroll, { passive: true })
}
componentWillUnmount() {
window.removeEventListener('scroll', this.handleScroll)
}
handleScroll(e) {
e.preventDefault();
// let offsetTop = this.myRef.current.offsetTop;
// here I'm trying just a console.log to preview the value
// otherwise my program will just crash
console.log("I'm scrolling, offsetTop: ", this.myRef)
}
render() {
return (
<div className={style.solution_container_layout} >
<div ref={this.myRef} className={style.solution_item}>
Run Code Online (Sandbox Code Playgroud)
任何提示都会很棒,谢谢
我正在尝试在 C++ 线程中执行对象的方法。
通过将方法的地址和对象(或对象的地址,或 std::ref(my_obj))传递给线程的构造函数,我能够做到这一点。
我观察到,如果我传递对象,而不是对象的地址或 std::ref(my_obj),那么对象会被复制两次(我正在复制构造函数中打印一些信息以查看)。
这是代码:
class Warrior{
string _name;
public:
// constructor
Warrior(string name): _name(name) {}
// copy constructor (prints every time the object is copied)
Warrior(const Warrior & other): _name("Copied " + other._name){
cout << "Copying warrior: \"" << other._name;
cout << "\" into : \"" << _name << "\"" << endl;
}
void attack(int damage){
cout << _name << " is attacking for " << damage << "!" << endl;
}
};
int …Run Code Online (Sandbox Code Playgroud) 我正在尝试在 React 中使用画布。下面的代码应该给我一个黑色的 800x800 画布,但由于某种原因它忽略了我的尺寸并显示了一个 300x150 的画布。
import React, {Component} from "react";
class Sample extends Component {
state = {
canvasWidth: 800,
canvasHeight: 800
}
canvasRef = React.createRef();
componentDidMount() {
const canvas = this.canvasRef.current;
const context = canvas.getContext('2d');
context.fillRect(0, 0, this.state.canvasWidth, this.state.canvasHeight);
}
render() {
return (
<div>
<canvas ref={this.canvasRef} />
</div>
);
}
}
export default Sample
Run Code Online (Sandbox Code Playgroud) 目标功能:
当用户单击按钮时,会显示一个列表。当他在列表外单击时,它会关闭并且按钮应该获得焦点。(遵循可访问性指南)
我试过的:
const hideList = () => {
// This closes the list
setListHidden(true);
// This takes a ref, which is forwarded to <Button/>, and focuses it
button.current.focus();
}
<Button
ref={button}
/>
Run Code Online (Sandbox Code Playgroud)
问题:
当我检查hideList函数的作用域时,发现ref除了在 click 事件处理程序内部之外的任何地方都获得了对按钮的正确引用,它是{current: null}.
控制台输出:Cannot read property 'focus' of null
示例:
https : //codepen.io/moaaz_bs/pen/zQjoLK
- 单击按钮,然后单击外部并查看控制台。