Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8126715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:14:59+00:00 2026-06-06T07:14:59+00:00

Given the following types private class MyTestDummyClassValidationDef : ValidationDef<MyTestDummyClass> { … } public class

  • 0

Given the following types

private class MyTestDummyClassValidationDef : ValidationDef<MyTestDummyClass>
    {
        ...
    }

public class ValidationDef<T> : IValidationDefinition<T>, IConstraintAggregator, IMappingSource where T : class        
{        }

public interface IMappingSource
{
    IClassMapping GetMapping();
}

public interface IClassMapping
{
    Type EntityType { get; }
    ...
}

At configuration time I know all of the ValidationDefinitions; “MyTestDummyClassValidationDef ” above is an example of such a definition.

If you follow the inheritance / implementation trail, at the end is an EntityType that is exposed by IClassMapping.

As part of my validation infrastructure, various objects may be asked to validate themselves. The objects may or may not have a ValidationDef defined for them, either because validation doesn’t apply fo that object or the definition hasn’t been written yet. If an object is asked to validate itself and there is no definition then a runtime error will occur.

SO, what I am trying to is have a list of EntityTypes that I can use to check at runtime. If the object being asked to validate itself is not on the list then I can avoid the runtime error that would otherwise occur.

How might I do that?

Cheers,
Berryl

the code I was looking for

public EntityValidator(ValidatorEngine validatorEngine, IEnumerable<Type> defTypes) {

ValidationDefs = new List<Type>();
foreach (var type in defTypes) 
{
    if (type.BaseType.GetGenericTypeDefinition() != typeof(ValidationDef<>)) continue;

    var mappingSource = (IMappingSource) Activator.CreateInstance(type);
    var entityType = mappingSource.GetMapping().EntityType;
    ValidationDefs.Add(entityType);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T07:15:02+00:00Added an answer on June 6, 2026 at 7:15 am

    Ok, after clarification (see comments to the question), here is the way to find all implementations of ValidationDef in an assembly and create a list of values of their EntityType properties:

    List<Type> entityTypes = new List<Type>();
    
    foreach (Type type in Assembly.GetExecutingAssembly().GetTypes())
    {
        if (InheritsFromValidationDef(type))
        {
            IMappingSource mappingSource = (IMappingSource)Activator.CreateInstance(type);
            entityTypes.Add(mappingSource.GetMapping().EntityType);
        }
    }
    
    private static bool InheritsFromValidationDef(Type type)
    {
        Type baseType = type.BaseType;
        bool result = baseType != null && 
                      baseType.IsGenericType &&
                      baseType.GetGenericTypeDefinition() == typeof(ValidationDef<>);
        return result;
    }
    

    Few points to note here:

    • Types in the current assembly are being examined (Assembly.GetExecutingAssembly). Obviously, this may or may not be sufficient for your scenario.
    • BaseType returns the direct parent of the type being examined. Again, you may want to examine the type hierarchy a bit further up the inheritance chain.
    • It is assumed that all examined types have a parameterless constructor. Otherwise, Activator.CreateInstance bit won’t work.

    Although it is certainly possible to do what you ask, I would like to strongly emphasize that it is very likely that there is much, much simpler solution to your validation needs. From what you’ve told us of your solution, it is obvious that it has a few serious flaws:

    • How is it possible that you can invoke validation on objects that cannot be validated? I think that this is the root of all your sorrows.
    • Your inheritance hierarchy is far too complex and hard to understand. Why do you need so many interfaces?

    Perhaps try to create a separate question detailing your validation needs. I am sure that a simpler solution can be found.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following code : package core; public abstract class GeometricElement { private float
Given the following Class and Service layer signatures: public class PersonActionRequest { PersonVO person
Given the following two classes: public class ABC { public void Accept(Ordering<User> xyz) {
Given the following code: class foo; foo* instance = NULL; class foo { public:
Following two cases seem to work: public class A { private class B {
Given the following example class: public class MyClass { public string S { get;
Given the following classes: public class Class1<TObject> { protected void MethodA<TType>(Expression<Func<TObject, TType>> property, ref
I have the following helper class (simplified): public static class Cache { private static
Given the following code: public class RMAInfo { public enum RMAStatuses { Undefined =
We have the following class public class TemporalData<T> { private T data; private String

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.