我想构建一个检测对象更改的代理:
代码示例 1 -defineProperty
const me = {
name: "Matt"
}
const proxy = new Proxy(me, {
defineProperty: function(target, key, descriptor) {
console.log(`Property ${key} defined.`);
return Object.defineProperty(target, key, descriptor);
}
});
proxy // { name: 'Matt' }
proxy.name = "Mark";
// Property name defined.
// Mark
proxy.age = 20;
// Property age defined.
// 20
Run Code Online (Sandbox Code Playgroud)
代码示例 1 - 观察
proxy
有一个属性name
,这是我所期望的。name
属性告诉我name
已定义;不是我所期望的。age
属性告诉我age
已经定义;正如我所料。代码示例 2 - …
我正在编写一个需要在C#中准确划分BigInteger类的类.
例:
BigInteger x = BigInteger.Parse("1000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
BigInteger y = BigInteger.Parse("2000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
x /= y;
Console.WriteLine(x.ToString());
//Output = 0
Run Code Online (Sandbox Code Playgroud)
问题是作为一个整数,自然它不包含十进制值.我怎样才能克服这一点,得到0.5的实际结果(给出的例子).
PS解决方案必须能够准确地划分任何BigInteger,而不仅仅是示例!
考虑以下示例 - 这使用一个接口:
public interface IAlgorithm<TResult, TInput>
{
TResult Compute(TInput input);
}
class A : IAlgorithm<int, byte[]>
{
// Notice the use of params...not strictly what the interface specifies but it works.
public int Compute(params byte[] input)
{
// no sane developer would go to this length to prove a point
return input[0];
}
}
A inst = new A();
Console.WriteLine(inst.Compute(1, 2, 3));
// 1
Run Code Online (Sandbox Code Playgroud)
这个例子显示了一个接口,它的方法implementation(params byte[]
)deos与接口契约(byte[]
)不完全匹配......但是它有效!
考虑以下示例 - 它使用抽象类:
public abstract class Algorithm<TResult, TInput>
{ …
Run Code Online (Sandbox Code Playgroud) 我有一个.NET Core 1.0类库,它以.NET 4.6.1为目标,并引用了.NET标准库1.6.0和Identity Framework 2.2.1
project.json
{
"version": "1.0.0-*",
"dependencies": {
"Microsoft.AspNet.Identity.EntityFramework": "2.2.1",
"System.Runtime": "4.1.0",
"NETStandard.Library": "1.6.0"
},
"frameworks": {
"netstandard1.6": {
"imports": [
"net461"
]
}
}
}
Run Code Online (Sandbox Code Playgroud)
在我的项目中,我只是创建了身份模型,它扩展了基本的Identity Framework模型(用户,角色等).当我尝试编译时,会发生这种情况......
任何想法如何解决这个问题?
考虑以下Generic类:
public class Custom<T> where T : string
{
}
Run Code Online (Sandbox Code Playgroud)
这会产生以下错误:
'string'不是有效的约束.用作约束的类型必须是接口,非密封类或类型参数.
是否有另一种方法来约束我的泛型类可以使用哪些类型?
另外,我可以约束多种类型吗?
例如
T只能是string,int或byte
使用此示例:
Color.FromArgb(Int32,Int32,Int32)
根据指定的8位颜色值(红色,绿色和蓝色)创建颜色结构.alpha值隐式为255(完全不透明).尽管此方法允许为每个颜色分量传递32位值,但每个分量的值限制为8位.
如果每个组件的值限制为8位,那么他们为什么不使用Byte
而不是Int32
?
在更广泛的范围内,我发现人们使用Int32
非常普遍,即使是Int16
或者Byte
足够.是否有使用什么特别的原因Int32
一般过Int16
,Byte
等?
首先,我想这可以作为如何使用Bootstrap实现多级导航栏的一个很好的例子(这是我长期努力的事情)
其次,我有一个问题,涉及防止内容在导航栏上崩溃.
我的导航栏有三行,两个navbar-default和一个navbar-inverse,以及三个按钮,用于在折叠时控制每个部分:
码:
<nav class="navbar navbar-fixed-top">
<div class="navbar-default">
<div class="container">
<div class="navbar-header">
<button type="button"
class="navbar-toggle collapsed pull-left"
data-toggle="collapse"
data-target="#megaNav"
aria-expanded="false"
aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<button type="button"
class="navbar-toggle collapsed"
data-toggle="collapse"
data-target="#siteNav"
aria-expanded="false"
aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<button type="button"
class="navbar-toggle collapsed"
data-toggle="collapse"
data-target="#authNav"
aria-expanded="false"
aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">NewCo</a>
</div>
<div id="siteNav" class="navbar-collapse collapse">
<form class="navbar-form navbar-right">
<div …
Run Code Online (Sandbox Code Playgroud) 规格
根据Array.prototype.map()的MDN规范,应该像这样使用map ...
var new_array = arr.map(callback[, thisArg])
Run Code Online (Sandbox Code Playgroud)
问题
TypeScript有几个重载的map声明,这使得它很难extend Array<T>
.
我希望看到这个(在lib.d.ts中) ...
map<U>(callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): U[];
Run Code Online (Sandbox Code Playgroud)
但lib.d.ts也有这些......
map<U>(this: [T, T, T, T, T], callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): [U, U, U, U, U];
map<U>(this: [T, T, T, T], callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): [U, U, U, U];
map<U>(this: [T, T, T], callbackfn: …
Run Code Online (Sandbox Code Playgroud) 因此,定义了TypeScript中的索引签名:
字典
[key: string]: T
Run Code Online (Sandbox Code Playgroud)
排列
[index: number]: T
Run Code Online (Sandbox Code Playgroud)
这些可以包装成一些简单,可重用的类型:
type DictionaryIndex<T> = {
[key: string]: T
}
type ArrayIndex<T> = {
[index: number]: T
}
Run Code Online (Sandbox Code Playgroud)
现在我想将它们包装成单一类型.我试过这个:
type Index<TKey extends string|number, TValue> = {
[key: TKey]: TValue
}
Run Code Online (Sandbox Code Playgroud)
由于以下错误,这不会编译:
索引签名参数必须是"string"或"number"类型.
这不可能吗?
到底是为了什么?
因为
foo(obj: Index<string, Bar>)
foo(obj: Index<string, Bar> & Fooable<string>)
Run Code Online (Sandbox Code Playgroud)
看起来比
foo(obj: { [key: string]: Bar })
foo(obj: { [key: string]: Bar, canFoo: (foo: string) => Bar })
Run Code Online (Sandbox Code Playgroud) 鉴于以下代码......
type Indexable<TKey, TValue> = { [index: TKey]: TValue }
Run Code Online (Sandbox Code Playgroud)
这会产生以下错误:
索引签名参数类型必须是"字符串"或"数字".
有没有办法约束TKey
为'字符串'或'数字'?
c# ×5
.net ×4
generics ×3
typescript ×3
javascript ×2
.net-core ×1
biginteger ×1
constraints ×1
css ×1
division ×1
html ×1
interface ×1
object ×1
overloading ×1
overriding ×1
proxy ×1
types ×1