How To Test if a Type is Anonymous?
From http://www.liensberger.it/web/blog/?p=191:
private static bool CheckIfAnonymousType(Type type)
{
if (type == null)
throw new ArgumentNullException("type");
// HACK: The only way to detect anonymous types right now.
return Attribute.IsDefined(type, typeof(CompilerGeneratedAttribute), false)
&& type.IsGenericType && type.Name.Contains("AnonymousType")
&& (type.Name.StartsWith("<>") || type.Name.StartsWith("VB$"))
&& type.Attributes.HasFlag(TypeAttributes.NotPublic);
}
EDIT:
Another link with extension method: Determining whether a Type is an Anonymous Type
Determining whether a Type is an Anonymous Type
Even though an anonymous type is an ordinary type, you can use some heuristics:
public static class TypeExtension {
public static Boolean IsAnonymousType(this Type type) {
Boolean hasCompilerGeneratedAttribute = type.GetCustomAttributes(typeof(CompilerGeneratedAttribute), false).Count() > 0;
Boolean nameContainsAnonymousType = type.FullName.Contains("AnonymousType");
Boolean isAnonymousType = hasCompilerGeneratedAttribute && nameContainsAnonymousType;
return isAnonymousType;
}
}
Another good heuristic to be used is if the class name is a valid C# name (anonymous type are generated with no valid C# class names - use regular expression for this).
How can I determine if an object of anonymous type is empty?
Anonymous types do not provide operator overloads for ==
, although it wouldn't matter in this case since one of the arguments is typed object. However the C# compiler does provide Equals, GetHashCode, and ToString implementations.
Use the static object.Equals
, method which will do the appropriate null checks and then call the virtual Equals method on the first argument:
object.Equals(attributes, new { });
You could also cache a static instance if you were concerned about the overhead of an allocation for each comparison.
Checking whether an Anonymous Type equals to a specified type or not
It looks like you are trying to see if a type parameter (type
) is instantiated as a specific type (byte
). If so try the following
public bool byteTest() {
return typeof(byte) == typeof(type);
}
The name type
here refers to a generic type parameter. An anonymous type in C# refers to a value created via an anonymous type expression. Like so
var x = new { Name = "john", Age = 42 };
Note: To avoid confusion with the type Type
i would choose a more standard generic argument name like T
, TValue
, etc ...
Asserting for equality anonymous types
Even though the anonymous types are accessible in your test project, that doesn't mean they'll be used when you write new { ... }
.
If you look at actionResult.Value.GetType()
and expectedActionResult.Value.GetType()
I strongly suspect you'll see that they're different types from different assemblies.
The simplest workaround in this case is probably just to compare the resulting JSON instead.
How do I check if an anonymous object properties are not empty C#
I solved it like that:
public static bool IsAnyNullOrEmpty(object objectToBeChecked, params string[] parametersToBeChecked)
{
foreach (var obj in (IEnumerable)objectToBeChecked)
{
foreach (var pi in obj.GetType().GetProperties())
{
if (parametersToBeChecked.Contains(pi.Name))
{
var value = (string)pi.GetValue(obj);
if (string.IsNullOrEmpty(value))
{
return true;
}
}
}
}
return false;
}
This will take the object (anonymous object) and properties names which you want to check, this works for me.
Related Topics
Using JSON.Net to Return Actionresult
Proper JSON Serialization in MVC 4
Observablecollection and Threading
Are These Objects's References on the Stack or on the Heap
How to Set Conditional Breakpoints in Visual Studio
Unhandled Exceptions in Backgroundworker
How to Receive Plug & Play Device Notifications Without a Windows Form
Convert Dictionary<String, Object> to Anonymous Object
Possible to Calculate Md5 (Or Other) Hash with Buffered Reads
How to Prevent Blank Xmlns Attributes in Output from .Net's Xmldocument
How to Get JSON Response from a 3.5 Asmx Web Service
When Should I Use Gc.Suppressfinalize()
How to Programmatically Click a Button in Wpf
C# - Wcf - Inter-Process Communication
What's the Difference Between Anonymous Methods (C# 2.0) and Lambda Expressions (C# 3.0)