can configure custom behaviour of built-in compiler exceptions? | SoloLearn: Learn to code for FREE!


can configure custom behaviour of built-in compiler exceptions?

assuming that if u call a property, field or method of an object, if it hasn't it... instead of throw an exception, there is some way of return 0 or null, or execute some routine or event? (apart of implement that behaviour with a method of that class-object ) scenario: got a set of objects (independently if they got all the same inheritance family and level hierarchical level), and they have bool fields. (the bools respond with true or false to the affirmation of its identifier) to avoid that all the type objects got all the possible bools that exist in the whole group of all the rest of brother objects, every object got its own set of relevant bools, and the rest not. so we use an external call asking for its bools, but if are asked for a missing one, then instead of an exception... (understand it?) but the custom exception behaviour only apply for those objects "case"

9/27/2020 11:48:54 PM


12 Answers

New Answer


Kiwwi# I'm with Schindlabua as I'm not following the use case scenario and agree with his feedback. That said, I decided to do a little experimenting and believe I have a solution involving dynamic types. Let me know if this is what you were looking for. Review the class defined on Line 88 to set how to create properties at runtime is they don't exist. More specifically, look at the overridden method TryGetMember(). Everything else is to show different scenarios involving classes that inherit from the base dynamic class.


[Follow Up 1 of 5] Kiwwi# I'm glad I was able to help. Sorry for missing your follow up questions. I'll respond to each bullet separately. NOTE: I've regrouped a few of the related questions together. -------- * @$"" -------- There are a few different things going on here. a) @ is the verbatim prefix for string literals. I'm using this to allow for my string to recognize the line break character without having to use the escaped \n character. b) $ is the string interpolation token for injecting expressions into the string without having to explicitly use concatenation or some position based composite formatting method like string.format("{0}", name). c) Both tokens can be used together to enable verbatim string literals with interpolation.


[Follow Up 5 of 5] * DynamicObject * dynamic More importantly, you may want to learn about the Dynamic Language Runtime (DLR) and how it relates with the Common Language Runtime (CLR). It might not be a bad idea to make your way to the CLR .NET Fundamentals as well as reviewed in the links (and related sublinks below): These are the links I would focus on in your spare time. You'll revisit the articles embedded here many times over through the years and reach new epiphanies and discoveries while gaining new insights. IDE Intellisense should cover most of what you need regarding the .NET Standard Library.


I am not totally sure what you are doing to be honest. If you are trying to access a property that does not exist in the class definition your code should not compile. Unless you are using reflection, or `dynamic`? An easier solution might be, to give each object a field called `bools` which is a `Dictionary<string, bool>` and then you can do dictionary lookup. That being said you might want to look into ExpandoObjects, maybe that fits your usecase. You can also write classes that extend `DynamicObject` but that's almost never the right solution.


[Follow Up 2 of 5] -------- * base() -------- Inherited class members that are protected, internal, or public can be directly accessed via the base keyword. This is in contrast to the this keyword with accesses the immediate class members and any base members that aren't overridden. base() is used to call the base constructor from within the immediate classes constructor. Although class constructors can't be overridden, classes can reuse their base class constructors using this special method. -------- * "object" --> System.Object, right? -------- Yes... these are the same and are interchangeable. C# supports aliases for a set of Built-in .NET types.


[Follow Up 3 of 5] -------- * Also the reversed class order its a bit confusing. -------- I'm not sure if you are referring to the order in which the classes are defined in the file. If this what you're referring to, then the order doesn't matter. The C# compiler automatically resolves the class dependency tree when compiling to CIL bytecode. Also, each class would typically be defined in their own respective files. In this case, order wouldn't be something that could be controlled anyway. Having multiple classes in a single file makes no difference to the compiler. It all gets sorted out. For clarity, the inheritance hierarchy in this code is as follows: System.Object (The root base class in the type hierarchy.) ----> System.Dynamic.DynamicObject --------> SoloLearn.DynamicProps ------------> SoloLearn.Animal ----------------> SoloLearn.Bird ----------------> SoloLearn.Person


[Follow Up 4 of 5] -------- * Seem that what I need is know the standard library better, but as is huge, must learn under case necessity -------- Nah... These are more related to the C# language itself. I do recommend you spend time reviewing C# as a language without getting distracted with the standard .NET library. The link below is a great place to focus on just the C# language features. Pay special attention to the side nav links as these provide a great way to quickly jump around and explore the different features. No need to read everything or memorize. Just get familiar with what's available and revisit over time as needed. These links (and their related sub links) should cover most of what you should eventually be familiar with. ----


David Carroll thx 4 the feedback new concepts 4 me: * @$"" * DynamicObject *dynamic *base() Also the reversed class order its a bit confusing. "object" --> System.Object, right? Seem that what I need is know the standard library better, but as is huge, must learn under case necessity

+2 From outside the object, or in it?


David Carroll thx 4 the feedback I know of the existence of the official C# site, got it on PDF. Anyways thx 4 the links, it helps. my problem is that Im doing many things at the same time; learning 3D modelling, learning a 3D engine and its scripting language, agnostic programming and other things like the Quake engine family standards, project design, and some others. So I think the best is learning pills, a bit everyday. The DLR-CLR matters seems to be enough relevant to jump-in rightnow. thx for your time, you're my C# mentor!


🌀PresidentOfYes12🌀 both, mainly outside on C#


Yes. Ill try to develop outside mainly, then go to inside