powered by simpleCommunicator - 2.0.61     © 2026 Programmizd 02
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Форумы / C++ [игнор отключен] [закрыт для гостей] / Перевод с C# на С++/CLI
4 сообщений из 4, страница 1 из 1
Перевод с C# на С++/CLI
    #34698092
ramid
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Добрый день!

Мне нужно перевести код с C# на C++/CLI

C#
Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
100.
101.
102.
103.
104.
105.
106.
107.
108.
109.
110.
111.
112.
113.
114.
115.
116.
117.
118.
119.
120.
121.
122.
123.
124.
125.
126.
127.
128.
129.
130.
131.
132.
133.
134.
135.
136.
137.
138.
139.
	public class VirtualPropertyDescriptor : PropertyDescriptor {
		string fPropertyName;
		Type fPropertyType;
		bool fIsReadOnly;
		VirtualList fList;
		int fIndex;
		public VirtualPropertyDescriptor(VirtualList fList, int fIndex, string fPropertyName, Type fPropertyType, bool fIsReadOnly) : base(fPropertyName, null) {
			this.fPropertyName = fPropertyName;
			this.fPropertyType = fPropertyType;
			this.fIsReadOnly = fIsReadOnly;
			this.fList = fList;
			this.fIndex = fIndex;
		}
		public override bool CanResetValue(object component) {
			return false;
		}
		public override object GetValue(object component) {
			return fList.GetRowValue(component, fIndex);
		}
		public override void SetValue(object component, object val) {
			fList.SetRowValue(component, fIndex, val);
		}
		public override bool IsReadOnly {get { return fIsReadOnly; } }
		public override string Name { get { return fPropertyName; } }
		public override Type ComponentType { get { return typeof(VirtualList); } }
		public override Type PropertyType { get { return fPropertyType; } }
		public override void ResetValue(object component) {
		}
		public override bool ShouldSerializeValue(object component) { return true; }
	}
	public class VirtualList : IList, ITypedList {
		int fRecordCount;
		int fColumnCount;
		Hashtable fValues;
		PropertyDescriptorCollection fColumnCollection;
		bool fUseDataStore = false;
		public VirtualList() {
			fValues = new Hashtable();
			fRecordCount = 1000;
			fColumnCount = 1000;
			CreateColumnCollection();
		}
		public virtual Hashtable Values { get { return fValues; } }
		public virtual object GetRowKey(int rowIndex, int colIndex) {
			return string.Format("row {0}, col {1}", rowIndex, colIndex);
		}
		internal object GetRowValue(object row, int colIndex) {
			int fIndex = (int)row;
			if(!UseDataStore) return GetRowKey(fIndex, colIndex);
			return Values[GetRowKey(fIndex, colIndex)];
		}
		internal void SetRowValue(object row, int colIndex, object val) { 
			if(!UseDataStore) return;
			int fIndex = (int)row;
			Values[GetRowKey(fIndex, colIndex)] = val;
		}
		public virtual bool UseDataStore {
			get { return fUseDataStore; }
			set { fUseDataStore = value; }
		}
		public int RecordCount { 
			get { return fRecordCount; }
			set {
				if(value < 1) value = 0;
				if(RecordCount == value) return;
				fRecordCount = value;
			}
		}
		public int ColumnCount { 
			get { return fColumnCount; }
			set {
				if(value < 1) value = 0;
				if(ColumnCount == value) return;
				fColumnCount = value;
				CreateColumnCollection();
			}
		}
		protected virtual void CreateColumnCollection() {
			VirtualPropertyDescriptor[] pds = new VirtualPropertyDescriptor[ColumnCount];
			for(int n = 0; n < ColumnCount; n++) {
                if (n == 1)
                {
                    pds[n] = new VirtualPropertyDescriptor(this, n, string.Format("Column{0}", n + 1), typeof(bool), false);
                }
                else
                {
                    pds[n] = new VirtualPropertyDescriptor(this, n, string.Format("Column{0}", n + 1), typeof(string), false);
                }
			}
			fColumnCollection = new PropertyDescriptorCollection(pds);
		}
		
		#region ITypedList Interface
		PropertyDescriptorCollection ITypedList.GetItemProperties(PropertyDescriptor[] descs) { return fColumnCollection; }
		string ITypedList.GetListName(PropertyDescriptor[] descs) { return ""; }
		#endregion
		#region IList Interface
		public virtual int Count { get { return RecordCount; }
		}
		public virtual bool IsSynchronized { get { return true; }
		}
		public virtual object SyncRoot { get { return true; }
		}
		public virtual bool IsReadOnly{ get { return false; } 
		}
		public virtual bool IsFixedSize{ get { return true; } 
		}
		public virtual IEnumerator GetEnumerator() { 
			return null;
		}
		public virtual void CopyTo(System.Array array, int fIndex) {
		}
		public virtual int Add(object val) {
			throw new NotImplementedException();
		}
		public virtual void Clear() {
			throw new NotImplementedException();
		}
		public virtual bool Contains(object val) {
			throw new NotImplementedException();
		}
		public virtual int IndexOf(object val) {
			throw new NotImplementedException();
		}
		public virtual void Insert(int fIndex, object val) {
			throw new NotImplementedException();
		}
		public virtual void Remove(object val) { 
			throw new NotImplementedException();
		}
		public virtual void RemoveAt(int fIndex) {
			throw new NotImplementedException();
		}
		object IList.this[int fIndex] { 
			get { return fIndex; }  
			set	{ }
		}
		#endregion
	}

Вот мой перевод на С++
Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
100.
101.
102.
103.
104.
105.
106.
107.
108.
109.
110.
111.
112.
113.
114.
115.
116.
117.
118.
119.
120.
121.
122.
123.
124.
125.
126.
127.
128.
129.
130.
131.
132.
133.
134.
135.
136.
137.
138.
139.
140.
141.
142.
143.
144.
145.
146.
147.
148.
149.
150.
151.
152.
153.
154.
#pragma once

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Data;
using namespace DevExpress::XtraEditors;
using namespace DevExpress::XtraGrid;

namespace Art {
	ref class VirtualPropertyDescriptor;

	public ref class VirtualList : System::Collections::IList, ITypedList {
		int fRecordCount;
		int fColumnCount;
		Hashtable ^fValues;
		PropertyDescriptorCollection ^fColumnCollection;
		bool fUseDataStore;
	public: VirtualList() {
				fUseDataStore = false;
				fValues = gcnew Hashtable();
				fRecordCount =  1000 ;
				fColumnCount =  1000 ;
				CreateColumnCollection();
			}
	public: property virtual Hashtable^ Values { 
				Hashtable^ get() { return fValues; } 
			}
	public: virtual Object^ GetRowKey(int rowIndex, int colIndex) {
				return String::Format("row {0}, col {1}", rowIndex, colIndex);
			}
	public: Object^ GetRowValue(Object ^row, int colIndex) {
				int fIndex = (int)row;
				if(!UseDataStore) return GetRowKey(fIndex, colIndex);
				return Values[GetRowKey(fIndex, colIndex)];
			}
	public: void SetRowValue(Object ^row, int colIndex, Object ^val) { 
				if(!UseDataStore) return;
				int fIndex = (int)row;
				Values[GetRowKey(fIndex, colIndex)] = val;
			}
	public: property virtual bool UseDataStore {
				bool get() { return fUseDataStore; }
				void set(bool value) { fUseDataStore = value; }
			}
	public: property int RecordCount { 
				int get() { return fRecordCount; }
				void set(int value) {
					if(value <  1 ) value =  0 ;
					if(RecordCount == value) return;
					fRecordCount = value;
				}
			}
	public: property int ColumnCount { 
				int get() { return fColumnCount; }
				void set(int value) {
					if(value <  1 ) value =  0 ;
					if(ColumnCount == value) return;
					fColumnCount = value;
					CreateColumnCollection();
				}
			}
	protected: virtual void CreateColumnCollection() {
				   array<VirtualPropertyDescriptor^>^ pds = gcnew array<VirtualPropertyDescriptor^>(ColumnCount);
				   for(int n =  0 ; n < ColumnCount; n++) {
					   if (n ==  1 )
					   {
						   pds[n] = gcnew VirtualPropertyDescriptor(this, n, String::Format("Column{0}", n +  1 ), Type::GetType("System.Boolean"), false);
					   }
					   else
					   {
						   pds[n] = gcnew VirtualPropertyDescriptor(this, n, String::Format("Column{0}", n +  1 ), Type::GetType("System.String"), false);
					   }
				   }
				   fColumnCollection = gcnew PropertyDescriptorCollection(pds);
			   }

	public: virtual PropertyDescriptorCollection^ GetItemProperties(array<PropertyDescriptor^>^ listAccessors) { return fColumnCollection; }
	public: virtual String^ GetListName(array<PropertyDescriptor^>^ listAccessors) { return ""; }

	public: property virtual int Count { int get() { return RecordCount; }
			}
	public: property  virtual bool IsSynchronized { bool get() { return true; }
			}
	public: property  virtual Object^ SyncRoot { Object^ get() { return true; }
			}
	public: property  virtual bool IsReadOnly{ bool get() { return false; } 
			}
	public: property  virtual bool IsFixedSize{ bool get() { return true; } 
			}
	public: virtual System::Collections::IEnumerator^ GetEnumerator() { 
				return nullptr;
			}
	public: virtual void CopyTo(System::Array^ a, int fIndex) {
			}
	public: virtual int Add(Object^ val) {
				throw gcnew NotImplementedException();
			}
	public: virtual void Clear() {
				throw gcnew NotImplementedException();
			}
	public: virtual bool Contains(Object^ val) {
				throw gcnew NotImplementedException();
			}
	public: virtual int IndexOf(Object^ val) {
				throw gcnew NotImplementedException();
			}
	public: virtual void Insert(int fIndex, Object^ val) {
				throw gcnew NotImplementedException();
			}
	public: virtual void Remove(Object ^val) { 
				throw gcnew NotImplementedException();
			}
	public: virtual void RemoveAt(int fIndex) {
				throw gcnew NotImplementedException();
			}
	public: virtual property Object^ default [int] { 
				Object^ get(int value) { return value; }  
				void set(int index, Object^ value)	{ }
			}
	};

	public ref class VirtualPropertyDescriptor : PropertyDescriptor {
		String ^fPropertyName;
		Type ^fPropertyType;
		bool fIsReadOnly;
		VirtualList ^fList;
		int fIndex;

	public: VirtualPropertyDescriptor(VirtualList ^fList, int fIndex, String ^fPropertyName, Type ^fPropertyType, bool fIsReadOnly) : PropertyDescriptor(fPropertyName, nullptr) {
				this->fPropertyName = fPropertyName;
				this->fPropertyType = fPropertyType;
				this->fIsReadOnly = fIsReadOnly;
				this->fList = fList;
				this->fIndex = fIndex;
			}
	public: virtual bool CanResetValue(Object ^component) override {
				return false;
			}
	public: virtual Object^ GetValue(Object^ component)  override {
				return fList->GetRowValue(component, fIndex);
			}
	public: virtual void SetValue(Object^ component, Object^ val) override {
				fList->SetRowValue(component, fIndex, val);
			}
	public: virtual property bool IsReadOnly {bool get() override { return fIsReadOnly; } }
	public: property virtual String^ Name { String^ get() override { return fPropertyName; } }
	public: property virtual Type^ ComponentType { Type^ get() override { return fList->GetType(); } }
	public: property virtual Type^ PropertyType { Type^ get() override { return fPropertyType; } }
	public: virtual  void ResetValue(Object^ component) override {
			}
	public: virtual bool ShouldSerializeValue(Object^ component) override { return true; }
	};
}

При компиляции на строки
pds[n] = gcnew VirtualPropertyDescriptor(this, n, String::Format("Column{0}", n + 1), Type::GetType("System.Boolean"), false);
pds[n] = gcnew VirtualPropertyDescriptor(this, n, String::Format("Column{0}", n + 1), Type::GetType("System.String"), false);
fColumnCollection = gcnew PropertyDescriptorCollection(pds);
из функции CreateColumnCollection выдаются соответственно ошибки:

error C2512: 'Art::VirtualPropertyDescriptor' : no appropriate default constructor available
error C2512: 'Art::VirtualPropertyDescriptor' : no appropriate default constructor available
error C2664: 'System::ComponentModel::PropertyDescriptorCollection::PropertyDescriptorCollection(cli::array<Type> ^)' : cannot convert parameter 1 from 'cli::array<Type> ^' to 'cli::array<Type> ^'

Что тут нужно ещё поправить?
...
Рейтинг: 0 / 0
Перевод с C# на С++/CLI
    #34699199
ramid
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Нужно было функцию CreateColumnCollection вынести в cpp файл.
...
Рейтинг: 0 / 0
Перевод с C# на С++/CLI
    #34702905
AndreiNz
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
А зачем переводить? Вы можете использовать assembly созданное на C# из кода на C++/CLI. Так, в крайнем случае в теории.
...
Рейтинг: 0 / 0
Перевод с C# на С++/CLI
    #34703621
ramid
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Основной проект на C++, проще использовать один язык.
...
Рейтинг: 0 / 0
4 сообщений из 4, страница 1 из 1
Форумы / C++ [игнор отключен] [закрыт для гостей] / Перевод с C# на С++/CLI
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]