简体   繁体   English

C#-使新类不属于System.Object

[英]C# - Make a new class not part of System.Object

I have a huge code base and I recently made a change where I changed the type of a parameter from String to a custom class. 我有一个庞大的代码库,最近我做了一个更改,将参数的类型从String更改为自定义类。 On the next compile I got all the areas where the impact was, but areas where the input type was of type Object failed. 在下一次编译中,我得到了影响的所有区域,但是输入类型为Object类型的区域失败了。 for eg 例如

String str = "32"
int i = Convert.ToInt32(str)

Now I have changed String to a new custom type lets say MyCustomClass I would now want following code to fail on next compile 现在,我已经将String更改为新的自定义类型,可以说MyCustomClass我现在希望以下代码在下次编译时失败

MyCustomClass str = new MyCustomClass("32")
int i = Convert.ToInt32(str)

but it won't as Convert.ToInt32 also accepts type Object . 但是不会,因为Convert.ToInt32还接受Object类型。 Is there some way I can make a change in MyCustomClass that it's not considered Object anymore. 有什么办法可以使MyCustomClass更改为不再被视为Object Please note : Convert.ToInt32 is only used for sample I have many more such functions, so please focus your suggestion/answer to question asked. 请注意Convert.ToInt32仅用于示例,我还有更多此类功能,因此请将您的建议/答案集中在所问的问题上。

Override ToString() and IConvertible 覆盖ToString()IConvertible

You said in the comments that your intentions are to find places where your object, which had previously been treated as a string, and are now being treated as an object. 您在评论中说,您的意图是找到对象的位置,该对象以前被视为字符串,而现在被视为对象。

In these situations typically, the third-party code would call .ToString() on your object to get something which it can use. 在这些情况下,通常,第三方代码将在您的对象上调用.ToString()以获取它可以使用的东西。
So, Convert.ToInt32(str) is equivalent to Convert.ToInt32(str.ToString()) . 因此, Convert.ToInt32(str)等同于Convert.ToInt32(str.ToString())

If you implement ToString() and IConvertible to return whatever your old version of str looked like then it should continue to work in the same way as the old version. 如果实现ToString()和IConvertible来返回旧版本的str则它应继续以旧版本的方式工作。

Probably. 大概。

Sorry I know that is not the 100% perfect compile time answer you were looking for, but I think you also know very well that your MyCustomClass will always be considered object. 对不起,我知道这不是您要找的100%完美的编译时答案,但是我想您也非常了解MyCustomClass将始终被视为对象。

Possible compile time answer: 可能的编译时间答案:

  • Write a tool which uses reflection to iterate over every class/struct/interface in every system/third-party DLL. 编写一个使用反射来遍历每个系统/第三方DLL中的每个类/结构/接口的工具。

  • Output a load of CS files which contain all these same classes, but just throw NotImplementedException. 输出包含所有这些相同类的CS文件的负载,只是抛出NotImplementedException。
    (T4 could help you do this) (T4可以帮助您做到这一点)

  • Compile these classes into dummy.dll 将这些类编译为dummy.dll

  • Your .csproj now references only this one dummy.dll, instead of the real dlls. 您的.csproj现在仅引用此一个dummy.dll,而不是实际的dll。
    Your project should compile fine against the dummy dll. 您的项目应针对虚拟dll进行编译。

  • Look at your dummy.cs files and delete any use of object. 查看您的dummy.cs文件,并删除对对象的任何使用。

  • Re-compile... and suddenly you get a load of compile time errors showing you anywhere you are using an object. 重新编译...突然之间,您会收到大量编译时错误,向您显示使用对象的任何地方。

Impliment an implicit cast from MyCustomClass to String. 从MyCustomClass 隐式转换为String。

public static implicit operator string(MyCustomClass str)
{
    return "Legacy respresentation of str";
}

This allows the complier the choice of choosing ToInt32(Object) or ToInt32(String), and I bet it favours the later. 这允许编译器选择选择ToInt32(Object)或ToInt32(String),我敢打赌后者会偏向后者。

This way all your existing function calls will remain the same so you wont have to be concerned about third party implentation details. 这样,您所有现有的函数调用将保持不变,因此您不必担心第三方执行细节。

(Sorry, I am not at a computer right now so I can`t test that my assumtion is correct. If you do test this, be sure to consider extension methods, as they can affect the conpilers desision making in unexpected ways) (对不起,我现在不在电脑上,因此我无法测试我的假设是正确的。如果您对此进行测试,请务必考虑扩展方法,因为它们会以意外的方式影响编译器的设计工作)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM