如何将DataContracts列表转换为整数数组[] [英] How to convert a List of DataContracts to an to an Integer array[]

查看:83
本文介绍了如何将DataContracts列表转换为整数数组[]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我需要将数据合同列表转换为整数数组[]

从C#转换为整数在C ++ / CLI中使用array []然后再使用C ++。



能不能让我知道如何实现这个目标。



感谢你的帮助。



  //   topicDetails是数据合同 
CreateSubscriptions< List< TopicDetails>(topicDetails);

// 这里的TopicDetails是一个类(数据合约),如下所示。

public class TopicDetails
{
protected object baseObjectType; // string
protected 对象主题; // string

public TopicDetails();

[DataMember]
public object BaseObjectType {获得; set ; }
[DataMember]
public object TopicID {获得; set ; }

public static TopicDetails CreateTopic< T,mT>(IComparable< T> ; objectType,IComparable< mT> objectID);
}

解决方案

大家好,



以下代码正在努力满足要求。

请看一下。

将Datacontracts列表转换为整数数组。



< pre lang =c#> int [] data;

var obj = topicDetails; // topicDetails =新列表< TopicDetails> {}; // DataContracts列表{BaseObjectType =:EQUIPMENT,TopicID =42};
var serializer = new System.Runtime.Serialization.DataContractSerializer( typeof (List< TopicDetails>));

使用 var stream = new System.IO.MemoryStream())
{
serializer.WriteObject(stream,obj);

data = new int [stream.Length];
stream.Position = 0 ;

for int i = 0 ; i < data.Length; i ++)
data [i] = stream.ReadByte();
}


Hi All ,

I need to convert a List of data contracts to an integer array[]
from C# to an Integer array[] in C++/CLI and then to C++.

Could you please let me know how can I achive this.

Appreciate your help on this.

//topicDetails is a data-contract
CreateSubscriptions<List<TopicDetails>(topicDetails);

//Here TopicDetails is a class (data contract) as follows.

public class TopicDetails
{
        protected object baseObjectType;  //string
        protected object topic;            //string

        public TopicDetails();

        [DataMember]
        public object BaseObjectType { get; set; }
        [DataMember]
        public object TopicID { get; set; }

        public static TopicDetails CreateTopic<T, mT>(IComparable<T> objectType, IComparable<mT> objectID);
}

解决方案

Hi All,

The following code is working to satisfy the requirement.
Please take a look at it.
To convert List of Datacontracts to an integer array.

 int[] data;

var obj = topicDetails;// topicDetails= new List<TopicDetails> { };// List of DataContracts{ BaseObjectType = ":EQUIPMENT", TopicID = "42" };
           var serializer = new System.Runtime.Serialization.DataContractSerializer(typeof(List<TopicDetails>));

           using (var stream = new System.IO.MemoryStream())
           {
               serializer.WriteObject(stream, obj);

               data = new int[stream.Length];
               stream.Position = 0;

               for (int i = 0; i < data.Length; i++)
                   data[i] = stream.ReadByte();
           }


这篇关于如何将DataContracts列表转换为整数数组[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆