急,高分求救,分不够加,加完为止,UP有分:关于强类型集合的问题!详细见内容!
//.h
#pragma once
#using <mscorlib.dll>
using namespace System;
using namespace System::Collections;
__gc class DictionaryInheritor : public DictionaryBase
{
public:
DictionaryInheritor(void);
~DictionaryInheritor(void);
__property Object * get_Item( Object * key ); //语句1
__property void set_Item( Object * key, Object * value ); //语句2
__property String * get_Item( String * key ); //语句3
__property void set_Item( String * key, String * value ); //语句4
};
//.cpp
#include "StdAfx.h"
#include "dictionaryinheritor.h"
DictionaryInheritor::DictionaryInheritor(void)
{
}
DictionaryInheritor::~DictionaryInheritor(void)
{
}
Object * DictionaryInheritor::get_Item(Object * key) //语句1
{
IDictionary * pDictionary = get_Dictionary();
return pDictionary->get_Item(key);
}
void DictionaryInheritor::set_Item(Object * key, Object * value) //语句2
{
IDictionary * pDictionary = get_Dictionary();
pDictionary->set_Item(key,value);
}
String * DictionaryInheritor::get_Item(String * key) //语句3
{
IDictionary * pDictionary = get_Dictionary();
return ( static_cast<String*>(pDictionary->get_Item(key)) );
}
void DictionaryInheritor::set_Item(String * key, String * value) //语句4
{
IDictionary * pDictionary = get_Dictionary();
pDictionary->set_Item( key, value );
}
/////////////////////
在.h和.cpp文件中
如果注释掉语句3和语句4,编译正常,运行正常,
如果注释掉语句1和语句2,这编译时的错误信息:
DictionaryInheritor.h(44): error C3815: “System::Collections::IDictionary::get_Item”方法的返回类型必须与“DictionaryInheritor::set_Item”最后一个参数的类型匹配
DictionaryInheritor.h(44): error C3815: “System::Collections::IDictionary::get_Item”方法的返回类型必须与“DictionaryInheritor::set_Item”最后一个参数的类型匹配
我的目的是实现强类型集合,在运行时不用对加入的对象进行类型转换了,
在MSDN里有一个类 DiscoveryExceptionDictionary 是这样实现的:
public: __property Exception* get_Item(String* url);
public: __property void set_Item(String* url, Exception*);
而我为什么不能改变Object 为String ?
听说.NET有很多BUG,这是我的程序有问题还是.NET的BUG?
???????????????????????????????WHY??????????????????????????????