小编Iai*_*oat的帖子

使用JNA将Java类传递给void*参数

我在C中有一个函数,我试图用JNA调用Java :

int myCfunc(void *s, int *ls);
Run Code Online (Sandbox Code Playgroud)

根据JNA文档,void*需要com.sun.jna.Pointer传递给函数.在带有JNA的java中我相信上面的函数将包装如下:

public interface myWrapper extends Library{ 
    public int myCfunc(Pointer s, IntByReference ls);
}
Run Code Online (Sandbox Code Playgroud)

需要链接到指针并传入参数的对象s将是实现JNA结构的类,例如:

public class myClass extends Structure{
    public int x;
    public int y;
    public int z;
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,该参数ls是一个整数,表示以字节为单位的类的长度.Java没有sizeof函数,因此增加了额外的复杂性.我遇到的另一个主要问题是确保我正确地将对象的内容传递给本机内存并返回.

我的代码类似于以下内容:

import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.Structure;
import com.sun.jna.ptr.IntByReference;

public void foo(){
    myWrapper wrapper = (myWrapper) Native.loadLibrary("SomeDllWithLegacyCode", myWrapper.class);

    myClass myObj = new myClass();
    myObj.x = 1;
    myObj.y = 2;
    Pointer myPointer …
Run Code Online (Sandbox Code Playgroud)

java pointers jna

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

在编译时确保互斥接口?

我想,以确保两个接口都从不在编译时相同的类,类似于如何发现AttributeUsage检查定制在编译时属性.

例如:

[InterfaceUsage(MutuallyExclusive = typeof(B))]
interface A {
    //...
}

interface B {
    //...
}

class C : A, B { //should throw an error on compile time
    //...
}
Run Code Online (Sandbox Code Playgroud)

我显然可以在运行时使用反射来完成此操作,但我对编译时解决方案很感兴趣.

我想象一个可能不存在开箱即用 - 但有没有办法创建一个在编译时运行的自定义属性,就像AttributeUsage一样?

c# attributes custom-attributes compile-time

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

从Java String的末尾删除行尾字符

我有一个字符串,我只想使用Java从字符串的最后删除行尾字符

"foo\r\nbar\r\nhello\r\nworld\r\n"
Run Code Online (Sandbox Code Playgroud)

我想成为

"foo\r\nbar\r\nhello\r\nworld"
Run Code Online (Sandbox Code Playgroud)

(这个问题与问题593671类似,但不一样)

java regex string

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

附加到MemoryStream

我正在尝试将一些数据附加到流中.这适用于FileStream,但不是MemoryStream由于固定的缓冲区大小.

将数据写入流的方法与创建流的方法分开(我在下面的例子中大大简化了它).创建流的方法不知道要写入流的数据长度.

public void Foo(){
    byte[] existingData = System.Text.Encoding.UTF8.GetBytes("foo");
    Stream s1 = new FileStream("someFile.txt", FileMode.Append, FileAccess.Write, FileShare.Read);
    s1.Write(existingData, 0, existingData.Length);


    Stream s2 = new MemoryStream(existingData, 0, existingData.Length, true);
    s2.Seek(0, SeekOrigin.End); //move to end of the stream for appending

    WriteUnknownDataToStream(s1);
    WriteUnknownDataToStream(s2); // NotSupportedException is thrown as the MemoryStream is not expandable
}

public static void WriteUnknownDataToStream(Stream s)
{
   // this is some example data for this SO query - the real data is generated elsewhere and is …
Run Code Online (Sandbox Code Playgroud)

c# arrays stream

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

标准定义json文件扩展名?

是否有定义json文件扩展名的标准或规范?

我见过.json用过 - 这只是一种普遍接受的做法,还是以文件格式保存的json的某些标准体的要求?

json file-format specifications standards-compliance

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

在通用c#类中链接隐式运算符

对于以下通用c#类,我想将T转换为K:

public abstract class ValueType<T,K> : IValueType<T> where K : ValueType<T,K>,new()
{     
    public abstract T Value { get; set; }     

    public static implicit operator ValueType<T,K>(T val) 
    {         
        K k = new K();
        k.Value = val;
        return k;    
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我要实现直接运算符implicit operator K(T val),则会导致编译时错误(CS0556).

我以为我可以尝试链接隐式运算符:

public static implicit operator K(ValueType<T,K> vt){
    return (K)val;
}
Run Code Online (Sandbox Code Playgroud)

但是下面的例子仍然抱怨它无法转换:

public class Test : ValueType<int, Test>
{
    public override int Value{ get; set; }
}

Test t = 6; //complains it's unable to be …
Run Code Online (Sandbox Code Playgroud)

c# abstract-class operator-overloading

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

Nancy无法在自定义约定中找到静态内容

我已经设置了一个Nancy bootstrapper来提供来自非默认目录路径的静态内容(它是自托管的Nancy).

奇怪的是,以下内容适用于自定义View位置约定,但不适用于js或css静态内容约定(是的,这些位置都存在文件和文件夹!).我尝试解决此问题的尝试进一步复杂化,因为我还没有想出如何记录未找到静态内容时发生的错误.

using System;
using System.IO;

using Nancy;
using Nancy.Conventions;
using Nancy.Bootstrapper;
using Nancy.TinyIoc;

namespace MyApp
{
    public class ApplicationBootstrapper : DefaultNancyBootstrapper
    {

    private const string RELATIVE_PATH_TO_SOURCE = @"../static/MyApp/";

    protected override void ConfigureConventions(NancyConventions nancyConventions)
    {

        nancyConventions.StaticContentsConventions.Add(StaticContentConventionBuilder.AddDirectory("js", string.Concat(RELATIVE_PATH_TO_SOURCE, "Scripts/")));
        nancyConventions.StaticContentsConventions.Add(StaticContentConventionBuilder.AddDirectory("css", string.Concat(RELATIVE_PATH_TO_SOURCE, "Content/")));
        this.Conventions.ViewLocationConventions.Add((viewName, model, context) =>
        {
            return string.Concat(RELATIVE_PATH_TO_SOURCE, "Views/", viewName);
        });
        this.Conventions.ViewLocationConventions.Add((viewName, model, context) =>
        {
            return string.Concat(RELATIVE_PATH_TO_SOURCE, "Views/", context.ModuleName, "/", viewName);
        });

        base.ConfigureConventions(nancyConventions);
    }

    protected override IRootPathProvider RootPathProvider
    {
        get
        {
            return new MyRootPathProvider();
        }
    }

    protected override …
Run Code Online (Sandbox Code Playgroud)

nancy

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

C#在运行时为Array.SetValue类型转换

我正在尝试使用反射创建一个数组,并将值插入其中.我正在尝试为许多不同类型执行此操作,因此需要一个createAndFillArray能够执行此操作的函数:

String propertyName1 = "prop1";
String propertyName2 = "prop2";

Type t1 = typeof(myType).getProperty(propertyName1).PropertyType.getElementType();
Type t2 = typeof(myType).getProperty(propertyName2).PropertyType.getElementType();

double exampleA = 22.5;
int exampleB = 43;

Array arrA = createAndFillArray(t1, exampleA);
Array arrB = createAndFillArray(t2, exampleB);

private Array createAndFillArray(Type t, object val){
    Array arr = Array.CreateInstance( t, 1); //length 1 in this example only, real-world is of variable length.
    arr.SetValue( val, 0 ); //this causes the following error: "System.InvalidCastException : Object cannot be stored in an array of this …
Run Code Online (Sandbox Code Playgroud)

c# arrays reflection casting runtime

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

将C++移植到C# - 模板

我正在将C++应用程序移植到C#,并且已经跨模板运行.我已经阅读了一些这些,我知道有些模板类似于.Net泛型.我读了这个案例的SO答案,很好地总结了它.

但是,c ++模板的一些用法似乎与泛型没有直接关系.在以下维基百科的模板元编程文章示例中,模板似乎接受一个值,而不是一个类型.我不太确定如何移植到C#?

template <int N>
struct Factorial 
{
    enum { value = N * Factorial<N - 1>::value };
};

template <>
struct Factorial<0> 
{
    enum { value = 1 };
};

// Factorial<4>::value == 24
// Factorial<0>::value == 1
void foo()
{
    int x = Factorial<4>::value; // == 24
    int y = Factorial<0>::value; // == 1
}
Run Code Online (Sandbox Code Playgroud)

显然,对于这个例子,我可以这样做:

public int Factorial(int N){
    if(N == 0) return 1;
    return Factorial(N - 1);
}
Run Code Online (Sandbox Code Playgroud)

但在我看来,这似乎是对函数的重构,而不是语义类似代码的端口.

.net c# c++ templates porting

5
推荐指数
2
解决办法
1053
查看次数

在Node.js中存储三元组

我在Node.js中开发一个应用程序,我的数据以json表示三元组,如下所示:

{
    subject:"Hello",
    predicate:"Knows",
    object:"World"
}
Run Code Online (Sandbox Code Playgroud)

我希望能够以相对简单的方式将我的数据存储到[data | triple]商店中.

元组内的数据值也需要搜索.

我正在寻找使用现有的RDF服务器并通过HTTP访问它,但是希望避免使用Sparql(一种解决方法可能是使用一个单独的javascript模块来包装Sparql).

是否存在任何处理存储三元组的Node模块?如果没有,那么存储三元组的实用解决方案是什么?

triplestore node.js

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