我何时应该使用Marshal.FinalReleaseComObjectvs Marshal.ReleaseComObject?
使用有危险Marshal.FinalReleaseComObject吗?
如果struct包含DateTime字段,为什么LayoutKind.Sequential的工作方式不同?
请考虑以下代码(必须使用"unsafe"启用的编译器应用程序):
using System;
using System.Runtime.InteropServices;
namespace ConsoleApplication3
{
static class Program
{
static void Main()
{
Inner test = new Inner();
unsafe
{
Console.WriteLine("Address of struct = " + ((int)&test).ToString("X"));
Console.WriteLine("Address of First = " + ((int)&test.First).ToString("X"));
Console.WriteLine("Address of NotFirst = " + ((int)&test.NotFirst).ToString("X"));
}
}
}
[StructLayout(LayoutKind.Sequential)]
public struct Inner
{
public byte First;
public double NotFirst;
public DateTime WTF;
}
}
Run Code Online (Sandbox Code Playgroud)
现在如果我运行上面的代码,我得到类似于以下的输出:
struct = 40F2CC的
地址First = 40F2D4的
地址NotFirst的地址= 40F2CC
注意,First的地址与struct的地址不同; 然而,NotFirst的地址是一样的结构的地址.
现在注释掉结构中的"DateTime WTF"字段,然后再次运行它.这一次,我得到的输出类似于:
struct …
我需要在声明顺序的保证顺序中获取fieldinfo.现在我正在使用属性来指定顺序.
有更自动的方式吗?
有没有人知道如何LayoutKind.Sequential工作,如果我可以应用它的技术.
LayoutKind.Sequential除非有一些添加属性的预编译器代码,否则我看不出它是如何工作的.
我有一个映射器的Map-Reduce作业,它接受一个记录并将其转换为一个对象,一个MyObject的实例,它使用Jackson编组为JSON.该值只是记录中的另一个Text字段.
映射器的相关部分如下所示:
ObjectMapper mapper = new ObjectMapper();
MyObject val = new MyObject();
val.setA(stringA);
val.setB(stringB);
Writer strWriter = new StringWriter();
mapper.writeValue(strWriter, val);
key.set(strWriter.toString());
Run Code Online (Sandbox Code Playgroud)
映射器的输出被发送到组合器,组合器解组JSON对象并聚合键值对.它在概念上非常简单,如下所示:
public void reduce(Text key, Iterable<IntWritable> values, Context cxt)
throws IOException, InterruptedException {
int count = 0;
TermIndex x = _mapper.readValue(key.toString(), MyObject.class);
for (IntWritable int : values) ++count;
...
emit (key, value)
}
Run Code Online (Sandbox Code Playgroud)
MyObject类由两个字段(两个字符串),get/set方法和一个默认构造函数组成.其中一个字段存储基于Web爬网的文本片段,但始终是字符串.
public class MyObject {
private String A;
private String B;
public MyObject() {}
public String getA() {
return A;
}
public void setA(String A) …Run Code Online (Sandbox Code Playgroud) 给定DLL中的以下C函数:
char * GetDir(char* path );
Run Code Online (Sandbox Code Playgroud)
你将如何P/Invoke这个函数到C#并正确编组char*..NET似乎知道如何做LPCTSTR,但是当我无法找出任何不会导致NotSupportedException在调用此函数时触发的编组时.
我试图从C#调用以下简单的C函数:
SIMPLEDLL_API const char* ReturnString()
{
return "Returning a static string!";
}
Run Code Online (Sandbox Code Playgroud)
使用以下P/Invoke声明(使用或不使用return属性,它没有区别):
[DllImport("SimpleDll")]
[return: MarshalAs(UnmanagedType.LPStr)]
public static extern string ReturnString();
Run Code Online (Sandbox Code Playgroud)
如果DLL是Debug构建但在Release构建(AccessViolationException)中崩溃,则它可以工作.
我正在调用十几个其他简单的函数,这是唯一失败的函数(这里是其他函数:)
[DllImport("SimpleDll")] public static extern int NextInt();
[DllImport("SimpleDll")] public static extern void SetNextInt(int x);
[DllImport("SimpleDll")] public static extern int AddInts(int a, int b);
[DllImport("SimpleDll")] public static extern int AddFourInts(int a, int b, int c, int d);
[DllImport("SimpleDll")] public static extern double AddDoubles(double x, double y);
[DllImport("SimpleDll")] public static extern IntPtr AddDoublesIndirect(ref double x, ref double y);
[DllImport("SimpleDll")] [return: …Run Code Online (Sandbox Code Playgroud) 有没有人能够在使用JAXB的对象编组期间删除未使用的命名空间?以下是所请求功能的链接:https://github.com/javaee/jaxb-v2/issues/103(见说明)
是否有为此配置JAXB的属性?这是在MOXy中修复的吗?
我目前正在遍历需要编组的对象并提取所有需要绑定的类Class[] classesToBeBound.然后我创建一个新的JAXBContext.newInstance(classesToBeBound)
现在未使用的命名空间不包含在XML中.
我知道即使使用未使用的命名空间,xml验证也是有效的,但对我来说这是框架应该处理的东西.
以下链接https://blogs.oracle.com/enterprisetechtips/entry/customizing_jaxb提到各种修复(见文中间部分),但是当试图在这些链接中找到解决方案时,链接被破坏或者没有人真正解决它.
欢迎任何评论.
(编辑)纯文字:
GIVEN
a new instance of JAXBContext and add 2 classes with each a separate namespace.
Run Code Online (Sandbox Code Playgroud)
什么时候
marshalling a class that has these 2 classes as a property but only 1 of them is not null
Run Code Online (Sandbox Code Playgroud)
然后
I expect only the namespace of the property that is not null to be visible in the XML.
Run Code Online (Sandbox Code Playgroud)
但是ACTUAL是
that both namespaces are in the xml. …Run Code Online (Sandbox Code Playgroud) 我尝试从我的C#-application中调用来自外部DLL的普通C函数.此功能定义为
void set_param(const char *data)
Run Code Online (Sandbox Code Playgroud)
现在我使用这个函数有一些问题:
如何在C#代码中指定此"const"?public static extern void set_param(sbyte *data)似乎错过了"const"部分.
调用此函数时,如何移交普通的8位C字符串?调用set_param("127.0.0.1")导致错误消息"无法从'string'转换为'sbyte '"*.
我如何编组这个C++类型?
ABS_DATA结构用于将任意长的数据块与长度信息相关联.声明的Data数组长度为1,但实际长度由Length成员给出.
typedef struct abs_data {
ABS_DWORD Length;
ABS_BYTE Data[ABS_VARLEN];
} ABS_DATA;
Run Code Online (Sandbox Code Playgroud)
我尝试了以下代码,但它不起作用.数据变量总是空的,我确信它有数据.
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet = System.Runtime.InteropServices.CharSet.Ansi)]
public struct abs_data
{
/// ABS_DWORD->unsigned int
public uint Length;
/// ABS_BYTE[1]
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst = 1)]
public string Data;
}
Run Code Online (Sandbox Code Playgroud) 在为基于智能卡的加密狗开发软件保护库的过程中,我意识到我需要在加密狗内部的客户端应用程序和代码之间来回传输一些树状数据结构.
那么,在使用Web服务时,XML-RPC或JSON-RPC等技术是合理的考虑方式.但是,智能卡等嵌入式设备并非如此.您需要使用一些二进制格式来优化内存使用并获得良好的性能.
我想我需要的是实现一些二进制数据编组算法.我不喜欢重新发明整个轮子的想法,我非常确定有很多关于编组这类问题的书籍,文章和例子.
你会推荐什么?
UPD. 我在Linux上使用C和C++,但问题是关于编组算法的一般信息.
marshalling ×10
c# ×5
.net ×4
pinvoke ×3
c++ ×2
java ×2
c ×1
com ×1
datetime ×1
dll ×1
hadoop ×1
jackson ×1
jaxb ×1
json ×1
namespaces ×1
reflection ×1
rpc ×1
structlayout ×1
xml ×1