小编Jue*_*gen的帖子

C#ToCharArray不适用于char*

我有以下结构:

[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Unicode)]
unsafe public struct Attributes
{

    public OrderCommand Command { get; set; }

    public int RefID { get; set; }

    public fixed char MarketSymbol[30];
}
Run Code Online (Sandbox Code Playgroud)

现在,我想将字符写入MarketSymbol字段:

string symbol = "test";
Attributes.MarketSymbol = symbol.ToCharArray();
Run Code Online (Sandbox Code Playgroud)

编译器抛出一个错误,说它无法从char []转换为char*.我该怎么写呢?谢谢

c# arrays string pointers

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

C#EF 4.1在DbContext中动态创建表

我想在运行时将表添加到SQLCe数据库,因为表名不是静态的,并且在编译时是已知的。我尝试使用Entity Framework 4.1和DbContext做到这一点,如下所示:

public class PersonContext : DbContext
{
    public PersonContext()
        : base("UnicornsCEDatabase")
    {
    }
}
public class Person
{
    public int NameId { get; set; }
    public string Name { get; set; }
}
public class Program
{
    static void Main(string[] args)
    {
        using (var db = new PersonContext())
        {
            db.Database.Delete();

            //Try to create table
            DbSet per = db.Set<Person>();
            var per1 = new Person { NameId = 1, Name = "James"};
            per.Add(per1);
            int recordsAffected = db.SaveChanges();

            Console.WriteLine(
                "Saved {0} …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework dbcontext

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

JUnitParams不使用String数组

考虑这个测试类,使用JUnit 4和JUnitParams:

import static junitparams.JUnitParamsRunner.$;
import junitparams.JUnitParamsRunner;
import junitparams.Parameters;

import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(JUnitParamsRunner.class)
public class JUnitParamsExample {

    private int[] getIntArray() {
        int array[] = new int[2];
        array[0] = 1;
        array[1] = 2;
        return array;
    }

    public Object getInts() {
        return $($(getIntArray()));
    }

    @Parameters(method = "getInts")
    @Test
    public void testIntArray(int... values) {
        //
    }

    private String[] getStringArray() {
        String array[] = new String[2];
        array[0] = "a";
        array[1] = "b";
        return array;
    }

    public Object getStrings() {
        return $($(getStringArray()));
    }

    @Parameters(method …
Run Code Online (Sandbox Code Playgroud)

java junit parameterized-unit-test junitparams

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

Int-Pointer to unmanaged code

我对C#很新,并且有一个简单的(?)问题.从非托管代码我收到一个int指针:

public foo(ref IntPtr state)
{
   _myState = state;
}
Run Code Online (Sandbox Code Playgroud)

_myStateIntPtr班上的一员.现在我想通过_myState非托管C++代码交换状态.如果我写这个,一切都有效:

public foo(ref IntPtr state)
{
   _myState = state;
   ....do some stuff
   state = 7;
}
Run Code Online (Sandbox Code Playgroud)

在非托管应用程序中,我可以看到新值7.但如果我写这个:

public foo(ref IntPtr state)
{
   _myState = state;
   ...do some stuff
   _myState = 7;
}
Run Code Online (Sandbox Code Playgroud)

没有任何反应.状态的初始值是0,并且在更改myState7它时未在非托管应用程序中更新.如何将一个成员变量分配给_myStatestate参数作为"指针",那么当状态更新时,_myState也会更新?在C++中,指针没有问题......

好的,这是真正的代码:

[DllExport("QFX_InitializeInterfaceObject", CallingConvention = CallingConvention.StdCall)]
    public static void QFX_InitializeInterfaceObject(
        long windowHandle,
        ref IntPtr processState)
    {
        ChartWindowHandle = (IntPtr)windowHandle;
        OrderCommandProcessState = …
Run Code Online (Sandbox Code Playgroud)

c# pointers unmanaged reference

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

C#垃圾收集

说我们有:

public void foo()
{
   someRefType test = new someRefType ();
   test = new someRefType ();
}
Run Code Online (Sandbox Code Playgroud)

垃圾收集器对第一个堆对象做了什么?是否在新任务之前立即收集垃圾?一般机制是什么?谢谢,Juergen

c# heap garbage-collection

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

在Java 8中创建通用谓词

我是Java 8的新手,想知道是否可以将实例方法传递给另一个方法,而该方法又在lambda参数上调用它:

考虑这个课程:

import java.util.function.Predicate;

public class PredicateTest {

    private class SomeType {

        public boolean bar() {
            return true;
        }

        public boolean foo() {
            return true;
        }
    }

    public void someMethod() {
        Predicate<SomeType> firstPredicate = someType -> someType.bar();
        Predicate<SomeType> secondPredicate = someType -> someType.foo();
        //...
    }

    public Predicate<SomeType> getGenericPredicate(/* ?? what goes here ?? */) {
        Predicate<SomeType> predicate = someType -> someType./* ?? how to call passed instance method foo or bar? */
        return predicate;
    }
}
Run Code Online (Sandbox Code Playgroud)

someMethod()两个谓词 …

java generics lambda predicate java-8

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

C++ 17:从模板中的 Callable 推断签名

是否以及如何从 C++17 中的任何可调用函数中推断出签名类型?片段:

template <typename T>
struct MyFunction;

template <typename R, typename... Args>
struct MyFunction<R(Args...)>
{
};

template <typename Callable>
auto deduceSignature(Callable&& c)
{
    // using Signature = ???;
    // return MyFunction<Signature>{ ???};}
}
Run Code Online (Sandbox Code Playgroud)

我想在return语句中使用 use 类模板参数推导。在客户端代码中我想这样写:

std::int8_t freeFunction(std::int8_t x)
{
    return x;
}

auto c1 = deduceSignature(&freeFunction);
auto c2 = deduceSignature([](std::int8_t x){
    return x;
});
Run Code Online (Sandbox Code Playgroud)

c++ callable signature template-argument-deduction c++17

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

由于删除了模板化左值转换运算符,clang 或 gcc 中可能存在错误?

这是这个问题的后续。我使用了这段代码并使用了 clang trunk 和 gcc trunk:

struct A
{   
};

struct T
{
    A a;
    operator A() { return a; }
    
    template <typename T> operator const T&() = delete;
};

struct C
{   
    A a;
};

int main()
{
    C c;
    T t;

    c.a = t;
}
Run Code Online (Sandbox Code Playgroud)

演示

Clang 没有什么可抱怨的,但 gcc 有:

<source>: In function 'int main()':
<source>:23:11: error: use of deleted function 'T::operator const T&() [with T = A]'
   23 |     c.a = t;
      |           ^
<source>:10:27: note: …
Run Code Online (Sandbox Code Playgroud)

c++ gcc operators type-conversion language-lawyer

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

C#基类中未使用的字段

在prism应用程序中,我有一个像这样的模块定义:

[Module(ModuleName = "TestModule", OnDemand = true)]
public class Test :
    ModelBase,
    IModule
{
  ...
  moduleName = "TestModule";
  ...
};
Run Code Online (Sandbox Code Playgroud)

如您所见,这些模块将在运行时加载,其中许多模块具有不同的模块名称.

为了避免代码冗余,我编写了一个基类ModelBase,这些模型从中派生如下:

public class ModelBase:
{
    /// <summary>
    /// The module name
    /// </summary>        
    protected string moduleName;
    ...
}
Run Code Online (Sandbox Code Playgroud)

字符串moduleName从不在ModelBase中使用,因此我得到编译器警告CS0169,其中说明了这一点.我不喜欢压制警告,所以我想知道是否有更好的解决方案.

moduleName将使用派生类中的实际名称进行设置,如第1段所示.

问题是无法在ModelBase类中指定名称,因为此处不知道模块的名称.

人们可以写这个问题可能听起来很愚蠢

public class ModelBase:
{
    /// <summary>
    /// The module name
    /// </summary>        
    protected string moduleName = "";
    ...
}
Run Code Online (Sandbox Code Playgroud)

克服这个问题.

我想知道这个反复出现的问题是否存在"最佳实践".非常感谢

克林斯曼

c# field base-class

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

内联C ++中的私有方法

在每个示例中,我都看到关键字inline用于头文件中的公共类方法(或函数)。

在实现文件中内联私有类方法有什么特别的地方吗?

c++ inline

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