令我惊讶的是我能够做到这一点:
public abstract class BaseComponent
{
protected GameObject GameObject;
internal BaseComponent(GameObject gameObject)
{
GameObject = gameObject;
}
}
public abstract class BaseComponent<TComponent, TManager> : BaseComponent
where ...
{
}
Run Code Online (Sandbox Code Playgroud)
我有一个代理,代码如下:
Sub Initialize
MessageBox "AgentStart"
Print "AgentStart"
Dim ws As New NotesUIWorkspace
Dim s As New NotesSession
Dim db As NotesDatabase
Dim vItemsBySupplierSpec As NotesView
Dim Doc As NotesDocument
Dim DocsWithSameSupplierSpec As NotesDocumentCollection
Dim MatchingDoc As NotesDocument
Set Doc = ws.CurrentDocument.Document
If Len(Doc.ItemSupplierSpecification(0)) > 0 Then
' Check that this supplier specification isn't use anywhere else.'
Set db = s.CurrentDatabase
Set vItemsBySupplierSpec = db.GetView("vItemsBySupplierSpec")
Set DocsWithSameSupplierSpec = vItemsBySupplierSpec.GetAllDocumentsByKey(Doc.ItemSupplierSpecification(0), True)
Set MatchingDoc = DocsWithSameSupplierSpec.GetFirstDocument
Dim ItemsString As String
ItemsString = "The …Run Code Online (Sandbox Code Playgroud) 我有一个通用类GenericClass<T>,我想池化实例。
我有兴趣看看是否可以获得语法:
MyGenericPool = new GenericPool<GenericClass>();
// Or maybe it's MyGenericPool = new GenericPool<GenericClass<>>();
GenericClass<TGenericParam> GenericClassInstance =
MyGenericPool.Get<TGenericParam>();
Run Code Online (Sandbox Code Playgroud)
(我对泛型的理解是,不,我不能,别傻了,语法不存在/不起作用,但我对其他人的想法很感兴趣)。
我有点怀疑,因为从我对类型的理解来看,从类型系统的角度来看,它们并没有真正相关GenericClass<string>。GenericClass<int>
现在,我意识到我可以接近,即:
GenericClass<TGenericParam> GenericClassInstance =
GenericPool.Get<GenericClass<TGenericParam>>();
Run Code Online (Sandbox Code Playgroud)
然后把它GenericPool存储在Dictionary<Type, ObjectPool<object>>某个地方。
我有兴趣看看是否可以避免这样做。我不想每次作为调用者只更改泛型类型参数时都必须指定泛型类型。我还希望能够强制(编译时)所有进入我的对象GenericObjectPool<T>都是一组通用类型(T<>)。
我认为问题源于无法将泛型类型参数视为泛型本身。如果我能做到这一点(我已经可以了吗?)那么也许像下面这样的东西可能会起作用:
public class GenericClassPool<TGeneric> where TGeneric : class<>
{
private readonly Dictionary<Type, object> objectPools = new Dictionary<Type, object>();
private void EnsureObjectPoolExists<TGenericParam>()
{
if (!objectPools.ContainsKey(typeof(TGenericParam)))
{
objectPools.Add(typeof(TGenericParam), new ObjectPool<TGeneric<TGenericParam>>(() => Activator.CreateInstance(typeof(TGeneric<TGenericParam>)) as TGeneric<TGenericParam>));
}
}
private ObjectPool<TGeneric<TGenericParam>> …Run Code Online (Sandbox Code Playgroud) 在c#中,您可以使用创建与关键字相同的变量@languagekeyword.
我的问题是,这什么时候有用?这些好处何时会超过缺点?
可能重复:
为什么类字段不能是var?
我想避免为局部变量输入类似字段的复杂/长类型定义.
我想知道为什么不能这样做?
public class Foo
{
public var barField = new Dictionary<string, int>(); // Does not work
public void Method()
{
var barLocal = new Dictionary<string, int>(); // Works
}
}
Run Code Online (Sandbox Code Playgroud) 我有一些旧的游戏代码,我正在移植到XNA.我的所有对象都使用x/y(float)和Vector2来描述各种位置等.
除了语法之外,有一个很好的基于性能的原因将我的旧x/y类型单位转换为Vector2,或者我可以只做一个直接端口而不必担心它?
我正在使用ASP.NET MVC2.
我喜欢如何注释我的模型的字段并检查ModelState.IsValid.
检查它的明显原因是在进行验证时,但是如果将新的验证规则应用于模型,是否应该始终检查它?
这样你就不会忘记/需要检查控制器是否检查IsValid.
在所有情况下都有一些理由不进行此检查吗?也许只是当控制器动作有副作用(DB写入等)?
我有以下代码:
var posts = elementToAdd.find(".post");
posts.each(function(i, e) {
var elementHeight = e.height() // BANG!
});
Run Code Online (Sandbox Code Playgroud)
帖子包含许多匹配的元素,我试图循环并根据每个元素的高度执行逻辑,但是单个元素没有height属性,那么我该如何解决这个问题呢?
假设我有一个列表:
List<int> _arr = new List<int> {1, 3, 4};
Run Code Online (Sandbox Code Playgroud)
而且是一个目标 4
我想回到{1, 3}作为1 + 3 = 4和{4}作为4 = 4使用LINQ从给定的列表.
我怎么做?
c# ×7
agents ×1
asp.net-mvc ×1
class ×1
generics ×1
javascript ×1
jquery ×1
keyword ×1
linq ×1
lotus-notes ×1
lotusscript ×1
performance ×1
var ×1
xna ×1