Comments (1)
Hello. That's an interesting observation.
I did some short research (less then 10 minutes), and short my short answer: I don't know why its happening, the answer requires deep understanding of CLR (which I don;t have).
But I have some guess.
Consider this code:
Assert.AreEqual(typeof(string), typeof(String)); //1
Assert.IsTrue(typeof(String) == typeof(string)); //2
Assert.IsTrue(Object.ReferenceEquals(typeof(String), typeof(string))); //3
Assert.AreEqual(typeof(String), ObjectExtensions.Copy(typeof(String))); //this FAIL
The interesting thing is line 3, which tells us that though Type is class (and not struct), but typeof operator always returns same reference of Type when called multiple times "typeof(String)", so Type behaves like Type<T>
singleton object, maintaining single reference for each <T>
.
I compared states (all field values) of "typeof(String)" and "ObjectExtensions.Copy(typeof(String))" they are identical, except their object reference (pointer). So I suspect "==" Type operator compares references with some internal singleton reference of internal CLR metadata containing loaded classes information.
And if you are not interested in understating of what is happening here and just need a fix ignore all the text above and I think you can fix the problem by changing this code:
public static bool IsPrimitive(this Type type)
{
if (type == typeof(String)) return true;
if (type == typeof(Type)) return true;
return (type.IsValueType & type.IsPrimitive);
}
from net-object-deep-copy.
Related Issues (20)
- The call is ambiguous between the following methods or properties: 'System.ArrayExtensions.ArrayExtensions.ForEach... HOT 5
- StackOverflow when copying EF entity
- visited.Add() belongs directly after the clone
- No Overload for foreach that takes 1 arguments HOT 1
- StackOverflow when Copy() my Class which inherits from INotifyPropertyChanged HOT 2
- No overload for method 'ForEach' takes 1 arguments HOT 2
- Blows up on multi-dimensional arrays HOT 6
- Copying properties of type Expression
- Trying to clone objects containing type PropertyInfo cause stack overflow. HOT 2
- Primitive enums
- Is the repository maintained? HOT 1
- Inconsistent Dictionary HOT 1
- Using Copy on a simple boxed object doesn't clone the value HOT 2
- array copy problem
- Cloning array of reference type objects didn't clone array elements. HOT 1
- can't copy PropertyInfo HOT 2
- Event subscription list being copied HOT 12
- Where are the System.ArrayExtensions from?
- StackOverflow in InternalCopy HOT 10
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from net-object-deep-copy.