You may be trying to access this site from a secured browser on the server. Please enable scripts and reload this page.
Turn on more accessible mode
Turn off more accessible mode
Skip Ribbon Commands
Skip to main content
To navigate through the Ribbon, use standard browser navigation keys. To skip between groups, use Ctrl+LEFT or Ctrl+RIGHT. To jump to the first Ribbon tab use Ctrl+[. To jump to the last selected command use Ctrl+]. To activate a command, use Enter.
Site Actions
This page location is:
Ondrej Sevecek's Blog
Ondrej Sevecek's English Pages
Comments
Works unless one or both of o1 and o2 are not value types
Browse
Tab 1 of 2.
View
Tab 2 of 2.
Sign In
Edit
Item
Version History
Manage Permissions
Delete Item
Manage
Ondrej Sevecek's English Pages
Comments
: Works unless one or both of o1 and o2 are not value types
Engineering and troubleshooting by Directory Master!
This Site
This List
Home
Currently selected
Contacts
Quick Posts
Quick Launch
Libraries
Site Pages
Pictures
General
Lists
Links
Posts
Comments
Categories
Quick Posts
Discussions
Surveys
All Site Content
Sorry comments are disable due to the constant load of spam
2
Title
Works unless one or both of o1 and o2 are not value types
Author
Doug Kaye
Body
If you have
object o1 = new SomeObject();
object o2 = new AnotherObject();
then your solution will throw the following exception;
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException
Operator '==' cannot be applied to operands of type 'ConsoleApp2.A' and 'ConsoleApp2.B'
The only universal solution I've found is using reflections. Just pass your objects to the following function;
static new bool Equals(object o1, object o2)
{
System.Reflection.MethodInfo method = o1.GetType().GetMethod("Equals", new Type[] { o1.GetType() });
if (method == null) method = o1.GetType().GetMethod("Equals");
if (method != null)
{
return (bool)method.Invoke(o1, new object[] { o2 });
}
else
return false;
}
Attachments
Created at 28/09/2018 21:25 by
Last modified at 28/09/2018 21:25 by