小编Ser*_*-Tm的帖子

如何检查c#中动态匿名类型上是否存在属性?

我有一个匿名类型对象,我从一个方法接收动态,我想检查该对象上存在的属性.

....
var settings = new {
                   Filename="temp.txt",
                   Size=10
}
...

function void Settings(dynamic settings) {
var exists = IsSettingExist(settings,"Filename")
}
Run Code Online (Sandbox Code Playgroud)

我该如何实现IsSettingExist?

c# reflection dynamic .net-4.0 c#-4.0

112
推荐指数
7
解决办法
9万
查看次数

哪个更好?array,ArrayList或List <T>(在性能和速度方面)

我需要快速处理我的页面.要添加的值的计数将是动态的.

以上哪一个是首选?支持有正当理由.

编辑:例如:

string str = "a,b,c"; //Count of the number of elements in str is not fixed
string[] arr = str.Split(',');
Run Code Online (Sandbox Code Playgroud)

要么,

ArrayList al = new ArrayList();
al.Add(str.Split(','));
Run Code Online (Sandbox Code Playgroud)

c# arrays performance list arraylist

18
推荐指数
3
解决办法
5万
查看次数

GAMS与AMPL代数建模语言的比较

我有兴趣获得GAMS和AMPL用户关于每种语言的优缺点的意见.

modeling ampl gams-math

17
推荐指数
2
解决办法
7399
查看次数

C#5和异步​​计时器

是否有新的Timer API允许我这样做?

await timer.wait(500);
Run Code Online (Sandbox Code Playgroud)

基本上,要睡眠X ms然后继续执行其余的功能

.net c# timer async-await c#-5.0

13
推荐指数
2
解决办法
5935
查看次数

创建运行时确定类型的实例的最佳方法

在.NET 4中创建在运行时确定的类型实例的最佳方法是什么?

我有一个实例方法,虽然作用于BaseClass对象可能会被其派生类的实例调用.我需要创建this与方法中相同类型的另一个实例.为每个派生类重载Method是不切实际的,因为它相当复杂,并且更有效地保持单个实现.

public class BaseClass
{
     //constructors + properties + methods etc

     public SomeMethod()
     {
          //some code

          DerivedClass d = new DerivedClass(); //ideally determine the DerivedClass type at run-time
     }
}
Run Code Online (Sandbox Code Playgroud)

我已经阅读了一些关于反射或使用动态关键字但我没有这些经验.

c# reflection performance runtime dynamic

13
推荐指数
3
解决办法
1万
查看次数

如何使用Reflection.emit在运行时创建方法

我在运行时使用反射发射创建一个对象.我成功创建了字段,属性和获取设置方法.现在我想添加一个方法.为简单起见,我们假设该方法只返回一个随机数.如何定义方法体?

编辑:

是的,我一直在查看msdn文档以及其他参考文献,我开始对这些东西感兴趣.我看到上面的例子是如何添加和/或多重,但是如果我的方法正在做其他事情该怎么办.我如何定义"东西"假设我动态生成下面的类,我将如何创建GetDetails()方法的主体?

class TestClass
{
    public string Name  { get; set; }
    public int Size  { get; set; }

    public TestClass()
    {
    }

    public TestClass(string Name, int Size)
    {
        this.Name = Name;
        this.Size = Size;
    }

    public string GetDetails()
    {
        string Details = "Name = " + this.Name + ", Size = " + this.Size.ToString();
        return Details;
    }
}
Run Code Online (Sandbox Code Playgroud)

reflection.emit c#-4.0

9
推荐指数
1
解决办法
1万
查看次数

包装C++以在C#中使用

好吧,基本上我要包装一个大的C++项目(Recast),以便我可以在我的C#项目中使用它.

我一直试图这样做一段时间,这就是我到目前为止所做的.我正在使用C++/CLI来包装我需要的类,以便我可以在C#中使用它们.

但是,我的C#项目中还需要大量的结构和枚举.那么如何包装这些呢?

我现在使用的基本方法是将dllexport调用添加到本机c ++代码,编译为dll/lib,将此lib添加到我的C++/CLI项目并导入c ++头文件,然后将CLI项目编译为dll,最后添加此dll作为我的C#项目的参考.我感谢任何帮助.

这里有一些代码.由于C++项目非常庞大,我需要可管理的方法.

//**Native unmanaged C++ code
//**Recast.h

enum rcTimerLabel
{
   A,
   B,
   C
};

extern "C" {

class __declspec(dllexport) rcContext
{
   public:
   inline rcContect(bool state);
   virtual ~rcContect() {}
   inline void resetLog() { if(m_logEnabled) doResetLog(); }

   protected:
   bool m_logEnabled;
}

struct rcConfig
{
   int width;
   int height;
}

} // end of extern


// **Managed CLI code
// **MyWrappers.h
#include "Recast.h"

namespace Wrappers
{
   public ref class MyWrapper
   {
   private:
     rcContect* _NativeClass;
   public:
     MyWrapper(bool …
Run Code Online (Sandbox Code Playgroud)

c# c++ dll c++-cli wrapper

8
推荐指数
1
解决办法
8285
查看次数

代表作为价值的字典

我有下课

public class CVisitor : IVisitor
    {
        public int Visit(Heartbeat element)
        {
            Trace.WriteLine("Heartbeat"); 
            return 1;
        }
        public int Visit(Information element)
        {
            Trace.WriteLine("Information"); 
             return 1;
        }

    }
Run Code Online (Sandbox Code Playgroud)

我希望有一个带映射的Dictionary,每个参数类型都将映射到它的实现函数:Heartbeat将被映射到 public int Visit(Heartbeat element)

我想做以下事情:

    _messageMapper = new Dictionary<Type, "what should be here ?" >();
    _messageMapper.Add(typeof(Heartbeat), "and how I put it here?" );
Run Code Online (Sandbox Code Playgroud)

我该怎么说"应该在这里?" 和"我怎么把它放在这里?"

谢谢

.net c#

8
推荐指数
1
解决办法
2万
查看次数

遍历任意维度的数组

使用c#,如何遍历未知维度的多维数组?

例如,考虑将数组的每个元素设置为指定值,需要迭代数组的所有条目.该方法应处理以下所有情况,并使用值4填充所有条目,而不管传递的数组的大小.

ClearArray(new int[3], 4);
ClearArray(new int[3,3], 4);
ClearArray(new int[3, 3, 3, 3], 4);
Run Code Online (Sandbox Code Playgroud)

方法签名显然看起来像

static void ClearArray(Array a, int val) { ... }
Run Code Online (Sandbox Code Playgroud)

我知道如何迭代一个维度:

for (int i=0; i<a.GetLength(dimension); i++) 
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

注意:这个问题不是关于2D数组,3D数组,也不是4D数组.它应该处理对象Rank上的Array属性所说的任何维度.

c# arrays algorithm performance

5
推荐指数
3
解决办法
2884
查看次数

while()没有{}?

我在VS2010的一个项目中有这个代码 - 它是一个我尚未完全实现的占位符方法.我今天开始实施.请注意,while语句的if/else周围没有{}.这编译了很多次 - 这已经有一段时间了.这是VS中的一个错误吗?我认为循环都需要{}

private void ParsefCIPProfiles(string block)
{
 StringReader reader = new StringReader(block);
 string readline = reader.readline();

 while (readline != null)
  if ()
  {}
  else
  {}
}
Run Code Online (Sandbox Code Playgroud)

c#

4
推荐指数
1
解决办法
2591
查看次数

SyntaxRewriter.Visit*不访问类型*的所有节点

解决了: mike z是对的,我没有正确地调用基数来继续递归.谢谢,迈克

我正在使用Roslyn进行一些代码重写,通过实现一个SyntaxRewriter.

我遇到的奇怪的事情是,当覆盖时SyntaxNode.VisitInvocationExpression(InvocationExpressionSyntax),它不会访问InvocationExpressionSyntax树中的所有节点.(我认为所有SyntaxNode类型都是一样的)

例如,给定此调用表达式:

  controller.Add(5, 6).ToString();
Run Code Online (Sandbox Code Playgroud)

它只访问整个表达式的节点,即使有2个调用.

虽然我当然可以写一个递归函数或类似于解析子/嵌套的InvocationExpression节点,但这似乎不一致和不方便.
为什么不访问整个树中*类型的所有节点?

这是我的覆盖:

    public override SyntaxNode VisitInvocationExpression(InvocationExpressionSyntax node)
    {
        IdentifierNameSyntax ident = node.ChildNodes().OfType<IdentifierNameSyntax>().FirstOrDefault();
        if (ident == null)
            return node;//In my test case, the example above returns here when it's node is encountered.  Shouldn't this then allow the walker to continue deeper into the node,
                        // finding the deeper nested Invocations?

        string name = ident.PlainName;
        if (!TempStore.ConstructedInvocations.ContainsKey(name))//not replacing this then
            return node;

        InvocationExpressionSyntax newInvocation …
Run Code Online (Sandbox Code Playgroud)

c# roslyn

3
推荐指数
1
解决办法
822
查看次数

过滤列表数据C#

如何转换我的字符串列表:

List<string> appliedFilters = filterString.Split(',').Select(n => n).ToList();
Run Code Online (Sandbox Code Playgroud)

进入一个没有重复的列表?

我的字符串列表(appliedFilters)如下所示:

7-27,
2-37,
7-28,
9-18,
9-22,
9-80
Run Code Online (Sandbox Code Playgroud)

我需要在字符串的第一部分输出此列表而不重复,例如:

7-27-28, 2-37, 9-18-22-80
Run Code Online (Sandbox Code Playgroud)

我确定这里有一个LINQ查询,但无法弄清楚.

c# linq string list duplicates

1
推荐指数
1
解决办法
416
查看次数